CopyTo not working for DataGrid

T

tshad

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::DataGrid
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>
</asp:DataGrid>

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
 
T

tshad

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
 
T

tshad

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>

<asp:DataGrid 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>

</asp:DataGrid>

<br /><br />

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

</form>

</body>
</html>

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top