I have a datagrid on the asp.net page. I need to generate the column name of the datagrid when the user clicking the button. I found it on the web ASP.Net Datagrid Get Column Index from Column Name but it doesn't work. The total of the datagrid column is 0. There are many code example for datagridview, but I am using datagrid. Would someone tell me how to do it. Thanks in advance.
There is my code:
For Each c As DataControlField In dgrd.Columns
Dim stringname As String = c.HeaderText
Next
There is the binding on aspx page:
<asp:TemplateColumn HeaderText="Date">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Date", "{0:ddMMMyyyy}") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Number" HeaderText="Number" />
It looks like it needs As DataGridColumn
instead of As DataControlField
:
Dim colHdrs As New List(Of String)
For Each c As DataGridColumn In ItemsGrid.Columns
colHdrs.Add(c.HeaderText)
Next
' colHdrs now contains the header text for each column
If you assign the HeaderText to the same variable each time round the loop, it will end up with only the last value.
I need to generate the column name of the datagrid
Hum, not sure it at all common to generate the column name? Does not one usually setup, or define the column names in the markup and layout for the grid?
We don't normally "generate" or "create" the column name(s) in code - so that's a bit confusing here.
Would you have idea how I can get the header text.
Ok, as long as the data grid been loaded up, then you should be fine.
So, as a general rule, you can fill a data grid. Say our code behind is this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadGrid
End If
End Sub
Sub LoadGrid()
Using conn = New SqlConnection(My.Settings.TEST4)
Dim strSQL As String =
"SELECT ID, Fighter, Engine, Thrust, Description, ImagePath, FirstFlight FROM Fighters"
Using cmdSQL = New SqlCommand(strSQL, conn)
conn.Open()
Dim rstData As New DataTable
rstData.Load(cmdSQL.ExecuteReader)
DataGrid1.DataSource = rstData
DataGrid1.DataBind()
End Using
End Using
End Sub
And say the data grid markup is this:
<asp:DataGrid ID="DataGrid1" runat="server"
DataKeyField="ID" CssClass="table" AutoGenerateColumns="false">
<Columns>
<asp:BoundColumn DataField="Fighter" HeaderText="Fighter" />
<asp:BoundColumn DataField="Engine" HeaderText="Engine" />
<asp:BoundColumn DataField="Thrust" HeaderText="Thrust" />
<asp:BoundColumn DataField="Description" HeaderText="Description" />
<asp:BoundColumn DataField="FirstFlight" HeaderText="Introduced" DataFormatString="{0:MMM-dd-yyyy}" ItemStyle-Width="100px" />
<asp:TemplateColumn HeaderText="View">
<ItemTemplate>
<asp:ImageButton ID="btnImage" runat="server" Height="68px" Width="149px"
OnClick="btnImage_Click"
ImageUrl = '<%# Eval("ImagePath") %>' />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" CssClass="btn btn-info"/>
<br />
Ok, so now we have/get this:
And our button click code for the button below the grid to get the column names, would be this:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each o As DataGridColumn In DataGrid1.Columns
Debug.Print(o.HeaderText)
Next
End Sub
output:
Fighter
Engine
Thrust
Description
Introduced
View
So, you can get the columns, but you have to make sure the grid been filled up with data BEFORE you attempt to use the data grid.
And manybe you want a row click. So, lets add a plane jane asp.net button to the grid, say like this:
<asp:TemplateColumn HeaderText="row click">
<ItemTemplate>
<asp:Button ID="cmdRowClick" runat="server" CssClass="btn"
OnClick="cmdRowClick_Click" />
</ItemTemplate>
</asp:TemplateColumn>
So, now the grid looks like this:
And our button click for the row click? This works:
Protected Sub cmdRowClick_Click(sender As Object, e As EventArgs)
Dim btn As Button = sender
Dim gRow As DataGridItem = btn.NamingContainer
Debug.Print("Grid row (index) click = " & gRow.ItemIndex)
Debug.Print("Database PK row id = " & DataGrid1.DataKeys(gRow.ItemIndex))
Debug.Print("Fighter Name = " & gRow.Cells(0).Text)
End Sub
Output:
Grid row (index) click = 3
Database PK row id = 4
Fighter Name = Lockheed Martin F-35 Lightning II
Note how we can still get the database PK "id" value, but it is NOT displayed anywhere in the grid - we used the datakey setting for this, and it is a great feature, since then we don't have to expose, or show or hide the PK data base row id, but can still get that PK "id" value in code behind.
So, we could now navigate based on that database "ID" and say pass it on to the next page to display more information about that give thing selected (clicked on) in that row.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.