DataGrid columns created at runtime

G

Gary Blakely

I'm giving this post another try - it can't be too difficult for
everyone....

In the program below, the web page has dataGrid1. the only thing that has
been done to it at design time is to check the "Create columns automatically
at runtime" checkbox - nothing else.

The code below does indeed create the visual grid as expected. Furthermore
the Cell contents and Item count all exist and contain expected values.
However there is no columns array !! After the databind, for instance,
DataGrid1.Columns(0) does not exist. There is therefore, no way to even
reference the HeaderText (which visually shows) or any other attribute of
the column.

Why is this?


Public Class WebForm1
Inherits System.Web.UI.Page

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim SQLStr As String = "Select FirstName, LastName From
Employees"
Dim myConnection As New System.Data.SqlClient.SqlConnection
myConnection.ConnectionString = _
"workstation id=myMachine;user id=sa;data source=myMachine;" & _
"persist security info=True;initial
catalog=NorthWind;password=xxxxx"
myConnection.Open()
Dim cmd As New System.Data.SqlClient.SqlCommand(SQLStr,
myConnection)
Dim da As New System.Data.SqlClient.SqlDataAdapter
da.SelectCommand = cmd
Dim dt As New DataTable
da.Fill(dt)
DataGrid1.DataSource = dt
DataGrid1.DataBind()

'at this point DataGrid1.Columns(0) does not exist
'however...
'DataGrid1.items(0).cells(0).text = "Nancy"
'DataGrid1.items.Count = 9

End If
End Sub

End Class
 
G

Guest

If you refer to the MSDN documentation on the DataGrid Columns property you
would find the following note:

"Note Explicitly declared columns may be used in conjunction with
automatically generated columns. When using both, explicitly declared columns
will be rendered first, followed by the automatically generated columns.
Automatically generated columns are not added to the Columns collection."

To reference the Header text for automatically generated columns you would
need to add your own code to the method that handles the ItemCreated or
ItemDataBound events. I have many samples on my website that customize the
handling of those events: http://www.societopia.net/Samples

If you have a more specific task that you cannot figure out from reading
those samples, please post its detail.
 
G

Gary Blakely

I wanted to keep the example as simple as possible. What we actually want
to do is to make the automatically created columns non visible (Header and
cells). We can easily make the cells non visible in the ItemDataBound
event but the HeaderText can't be referenced because it's a property of the
DataGridColumn. (we have other columns that need the header text so not
having header text in the grid is not a solution)

So, how can HeaderText be referenced when the columns don't exist? I didn't
see any examples of that at your site.
 
G

Guest

Hi Gary,

The ItemDataBound event handler passes to you as a parameter the
DataGridItemEventArgs (let's call it "e") which you can use to access not
only the header cells but also the footer, the pager, the items and
alternating items etc.. as follows:

switch (e.Item.ItemType )
{
case ListItemType.Header:
//this would blank out the header text in the
first column
e.Item.Cells[0].Text = "";
break;
case ListItemType.Item:
//do something
break;
case ListItemType.AlternatingItem:
//do something
break;
case ListItemType.Footer:
//do something
break;
case ListItemType.Pager :
//do something
break;

}
 
G

Gary Blakely

Phillip,
That worked great. thanks for you help!

--
Regards,
Gary Blakely
Phillip Williams said:
Hi Gary,

The ItemDataBound event handler passes to you as a parameter the
DataGridItemEventArgs (let's call it "e") which you can use to access not
only the header cells but also the footer, the pager, the items and
alternating items etc.. as follows:

switch (e.Item.ItemType )
{
case ListItemType.Header:
//this would blank out the header text in
the
first column
e.Item.Cells[0].Text = "";
break;
case ListItemType.Item:
//do something
break;
case ListItemType.AlternatingItem:
//do something
break;
case ListItemType.Footer:
//do something
break;
case ListItemType.Pager :
//do something
break;

}

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Gary Blakely said:
I wanted to keep the example as simple as possible. What we actually
want
to do is to make the automatically created columns non visible (Header
and
cells). We can easily make the cells non visible in the ItemDataBound
event but the HeaderText can't be referenced because it's a property of
the
DataGridColumn. (we have other columns that need the header text so not
having header text in the grid is not a solution)

So, how can HeaderText be referenced when the columns don't exist? I
didn't
see any examples of that at your site.
 
G

Guest

Hi Philip, Gary

I've got two related queries. I want on of the auto generated columns to be
read only but can't figure out what code to put under

case ListItemType.Item:
//do something

ie I don't want it to render a text box when the edit button is clicked.

Also I wrote some code to automatically generate template columns which get
added at runtime.
This work ok in that the grid rendered correctly but the postback event does
not contain the template columns in the event arguments. Do any of you know
what is happening?

I wrote

private void CreateTemplateColumns()
{

DataTable dt = myDataSet.Tables[0];
for (int counter = 1; counter < dt.Counter.Count; counter++)
{
TemplateColumn tc = new TemplateColumn();
tc.ItemTemplate = new DataGridTemplate(ListItemType.Item,
dt.Columns[counter].ColumnName);
TemplateColumn tc = new TemplateColumn();
tc.EditItemTemplate = new DataGridTemplate(ListItemType.EditItem,
dt.Columns[counter].ColumnName);
this.MyDataGrid.Columns.Add(tc)
}

}

This worked and rendered correctly but




private void MyDataGrid_UpdateCommand(object source,
DataGridCommandEventArgs e)

{
foreach (TableCell cell in e.Item.Cells)
{
// e only contains explicitly declared columns and not the Template columns
}

}



--
Regards

Gordon


Phillip Williams said:
Hi Gary,

The ItemDataBound event handler passes to you as a parameter the
DataGridItemEventArgs (let's call it "e") which you can use to access not
only the header cells but also the footer, the pager, the items and
alternating items etc.. as follows:

switch (e.Item.ItemType )
{
case ListItemType.Header:
//this would blank out the header text in the
first column
e.Item.Cells[0].Text = "";
break;
case ListItemType.Item:
//do something
break;
case ListItemType.AlternatingItem:
//do something
break;
case ListItemType.Footer:
//do something
break;
case ListItemType.Pager :
//do something
break;

}

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Gary Blakely said:
I wanted to keep the example as simple as possible. What we actually want
to do is to make the automatically created columns non visible (Header and
cells). We can easily make the cells non visible in the ItemDataBound
event but the HeaderText can't be referenced because it's a property of the
DataGridColumn. (we have other columns that need the header text so not
having header text in the grid is not a solution)

So, how can HeaderText be referenced when the columns don't exist? I didn't
see any examples of that at your site.
 

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,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top