Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > CopyTo not working for DataGrid

Reply
Thread Tools

CopyTo not working for DataGrid

 
 
tshad
Guest
Posts: n/a
 
      08-28-2007
I am having a real problem getting some of these methods to work.

I am trying to do a CopyTo and I can't get it to copy into my array.

I tried:

Sub ReorderDataGrid()
Dim theArray(1) as DataGridColumn
DataGrid1.Columns.CopyTo(theArray,0)
End Sub

And I get this:

System.IndexOutOfRangeException: Index was outside the bounds of the array

The error is on the CopyTo statement.

My DataGrid looks like:

<asp:ataGrid
Visible=true
AllowSorting="true"
AutoGenerateColumns="false"
CellPadding="0"
CellSpacing="0"
ID="DataGrid1"
runat="server"
ShowFooter="false"
ShowHeader="true"
OnSortCommand="SortDataGrid"
BorderWidth="0"
BorderColor="#999999"
style="padding-right:5px; width:573px">
<headerstyle Font-Bold="true" />
<alternatingitemstyle CssClass="alternateRows" />
<footerstyle BackColor="#E8EBFD" ForeColor="#3D3DB6" Font-Bold="true"
/>
<pagerstyle BackColor="white" />
<columns>
<asp:TemplateColumn sortexpression="JobTitle" ItemStyle-Width="190"
HeaderStyle-Width="190"
headertext="Job Title" ItemStyle-VerticalAlign="Top"
runat="server">
<ItemTemplate>
<asp:HyperLink ID="JobTitle"
NavigateURL='<%# "displayPositionNS.aspx?PositionID=" &
Container.DataItem("PositionID") %>'
Text='<%# Container.DataItem("JobTitle")%>'
OnPreRender="FixHyperLink"
runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn ItemStyle-Width="100" DataField="Location"
HeaderText="Location"
ReadOnly="true"
Visible="True"
ItemStyle-VerticalAlign="Top"
SortExpression="Location"/>
<asp:BoundColumn ItemStyle-Width="40" DataField="PositionType"
HeaderText="Type"
ReadOnly="true"
Visible="True"
ItemStyle-VerticalAlign="Top"
SortExpression="PositionType"/>
</columns>
</aspataGrid>

If I do:

Sub ReorderDataGrid()
Dim theArray() as DataGridColumn
DataGrid1.Columns.CopyTo(theArray,0)
End Sub

And I get this:

System.NullReferenceException: Object reference not set to an instance of an
object.

I tried running the ReorderDataGrid() from Page_Load before and after
Binding the Grid as well as in the Page_PreRender with the same results.

But this is how the examples I have found on the web have it.

What am I missing?

Thanks,

Tom


 
Reply With Quote
 
 
 
 
tshad
Guest
Posts: n/a
 
      08-28-2007
I copied this file from the msdn site and made some changes to both change
the headers and insert one of the columns that is in the array I got from my
CopyTo method.

Neither of these seem to work. The array still looks the same.

I tried putting the submit code in the PreRender as well and couldn't seem
to get that to work either.

Does anyone know what is missing?

Thanks,

Tom


 
Reply With Quote
 
 
 
 
tshad
Guest
Posts: n/a
 
      08-28-2007
Sorry,

Forgot to add the file:

<%@ Page Language="VB" trace=true AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<script runat="server">

Function CreateDataSource() As ICollection

' Create sample data for the DataGrid control.
Dim dt As DataTable = New DataTable()
Dim dr As DataRow

' Define the columns of the table.
dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn("StringValue", GetType(string)))
dt.Columns.Add(New DataColumn("CurrencyValue", GetType(double)))

' Populate the table with sample values.
Dim i As Integer

For i = 0 to 4

dr = dt.NewRow()

dr(0) = i
dr(1) = "Item " & i.ToString()
dr(2) = 1.23 * (i + 1)

dt.Rows.Add(dr)

Next i

Dim dv As DataView = New DataView(dt)
Return dv

End Function

Sub Page_Load(sender As Object, e As EventArgs)

' Load sample data only once, when the page is first loaded.
If Not IsPostBack Then

ItemsGrid.DataSource = CreateDataSource()
ItemsGrid.DataBind()

End If

End Sub

Sub Button_Click(sender As Object, e As EventArgs)

Dim myArray(3) As DataGridColumn
Dim ktr as Integer

ItemsGrid.Columns.CopyTo(myArray, 0)

Dim column As DataGridColumn
ktr = 0
For Each column In myArray
ItemsGrid.Columns(ktr).HeaderText = column.HeaderText & " New"
ktr+=1
Next column
ItemsGrid.Columns.AddAt(1,myArray(1))
End Sub 'Button_Click
</script>

<head runat="server">
<title>DataGridColumn Visible Example</title>
</head>
<body>

<form id="form1" runat="server">

<h3>DataGridColumn Visible Example</h3>

Select whether to show or hide the first column.

<br /><br />

<b>Product List</b>

<aspataGrid id="ItemsGrid"
BorderColor="black"
BorderWidth="1"
CellPadding="3"
ShowFooter="True"
AutoGenerateColumns="False"
runat="server">

<HeaderStyle BackColor="#00aaaa">
</HeaderStyle>

<FooterStyle BackColor="#00aaaa">
</FooterStyle>

<Columns>

<asp:BoundColumn DataField="IntegerValue"
Visible="True"
HeaderText="Item"/>

<asp:BoundColumn DataField="StringValue"
Visible="True"
HeaderText="Description"/>

<asp:BoundColumn DataField="CurrencyValue"
Visible="True"
HeaderText="Price"
DataFormatString="{0:c}">

<ItemStyle HorizontalAlign="Right">
</ItemStyle>

</asp:BoundColumn>

<asp:TemplateColumn HeaderText="Select Item"
Visible="True" >

<ItemTemplate>

<asp:CheckBox id="SelectCheckBox"
Text="Add to Cart"
Checked="False"
runat="server"/>

</ItemTemplate>

</asp:TemplateColumn>

</Columns>

</aspataGrid>

<br /><br />

<asp:Button id="SubmitButton"
Text="Submit"
OnClick = "Button_Click"
runat="server"/>

</form>

</body>
</html>

Tom

"tshad" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I copied this file from the msdn site and made some changes to both change
>the headers and insert one of the columns that is in the array I got from
>my CopyTo method.
>
> Neither of these seem to work. The array still looks the same.
>
> I tried putting the submit code in the PreRender as well and couldn't seem
> to get that to work either.
>
> Does anyone know what is missing?
>
> Thanks,
>
> Tom
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
wifi not working on new hp, or not working after live update =?Utf-8?B?RHJhZ29ueA==?= Wireless Networking 1 10-01-2005 11:17 PM
Cookies working on intranet but NOT working on Internet Martin Heuckeroth ASP .Net 5 04-01-2005 01:37 AM
Regular Expression validators NOT working, Required Field validators ARE working Ratman ASP .Net 0 09-14-2004 09:36 PM
To all Gurus: How can I edit/update a DataGrid in a DataGrid (nested DataGrid)? Possible? Andreas Klemt ASP .Net Datagrid Control 0 10-08-2003 01:19 AM
DirectoryEntry and CopyTo function Mik ASP .Net 0 07-04-2003 10:46 AM



Advertisments