By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,771 Members | 1,741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,771 IT Pros & Developers. It's quick & easy.

DataGrid columns created at runtime

P: n/a
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

--
Regards,
Gary Blakely
Dean Blakely & Associates
www.deanblakely.com
Nov 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Gary Blakely" wrote:
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

--
Regards,
Gary Blakely
Dean Blakely & Associates
www.deanblakely.com

Nov 19 '05 #2

P: n/a
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.

--
Regards,
Gary Blakely

"Phillip Williams" <Ph**************@webswapp.com> wrote in message
news:26**********************************@microsof t.com...
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.
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Gary Blakely" wrote:
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

--
Regards,
Gary Blakely
Dean Blakely & Associates
www.deanblakely.com

Nov 19 '05 #3

P: n/a
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" wrote:
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.

--
Regards,
Gary Blakely

"Phillip Williams" <Ph**************@webswapp.com> wrote in message
news:26**********************************@microsof t.com...
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.
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Gary Blakely" wrote:
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

--
Regards,
Gary Blakely
Dean Blakely & Associates
www.deanblakely.com


Nov 19 '05 #4

P: n/a
Phillip,
That worked great. thanks for you help!

--
Regards,
Gary Blakely
"Phillip Williams" <Ph**************@webswapp.com> wrote in message
news:7A**********************************@microsof t.com...
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" wrote:
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.

--
Regards,
Gary Blakely

"Phillip Williams" <Ph**************@webswapp.com> wrote in message
news:26**********************************@microsof t.com...
> 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.
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "Gary Blakely" wrote:
>
>> 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
>>
>> --
>> Regards,
>> Gary Blakely
>> Dean Blakely & Associates
>> www.deanblakely.com
>>
>>
>>


Nov 19 '05 #5

P: n/a
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" wrote:
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" wrote:
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.

--
Regards,
Gary Blakely

"Phillip Williams" <Ph**************@webswapp.com> wrote in message
news:26**********************************@microsof t.com...
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.
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Gary Blakely" wrote:

> 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
>
> --
> Regards,
> Gary Blakely
> Dean Blakely & Associates
> www.deanblakely.com
>
>
>


Nov 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.