473,325 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

DataGrid columns created at runtime

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
5 2610
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jon | last post by:
Hello, I have a datagrid and the data in it is dynamically created at runtime... For iCounter = 0 To dataset.Tables(0).Columns.Count - 1 Dim objbc As New BoundColumn() With objbc .DataField...
5
by: AC | last post by:
Any reason i'm having trouble with: DataGrid1.Columns(7).ItemStyle.HorizontalAlign = HorizontalAlign.Right I'm trying to directly set the column alignment of a datagrid created at runtime.. ...
2
by: Thanh Nu | last post by:
Hi, I would like to hide a column in a web datagrid (with create columns automatically at runtime checked), and I cannot refer to the columns collection like this: DataGrid1.Columns(0).Visible...
3
by: CJ Smit | last post by:
Hi Guys I have a datagrid that are databound at runtime and columns are added automatically to it from the dataset. OnEdit Command renders all fields as editable, my problem is I need to specify...
12
by: Daniel Walzenbach | last post by:
Hi, I want to display a Label in a DataGrid according to some condition. I therefore check whether the condition is true in the ItemDateBound EventHandler of the DataGrid. Unfortunately the...
2
by: Ben | last post by:
Hi, I'd like to have a datagrid that has a dropdownlist in the pager control for setting the page size. I can get the control into the pager inside the datagrid itemcreated event by checking for...
11
by: rkbnair | last post by:
I have created a datagrid in my aspx with the 'AllowSorting' property to true. When clicking on the column header, the page refreshes. However the sorting is not done. Am I missing anything? I...
4
by: gh | last post by:
I create the columns for the asp.net datagrid at runtime and populates them. I have a template column for checkboxs I created at runtime as well. The user selects the product they want by...
7
by: GaryDean | last post by:
(one of our developers posted this and it got no answer so I'm giving it another try) I'm converting a DataGrid utility component that previously used the columns array to function as it has in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.