In a web application, how can I find the controls in header, and how can I bind the value dynamically to those controls? This is what my gridview looks like...
----------------------------------------------
| October | November |
product| | |
| s开发者_C百科elf | Officer | self | officer|
----------------------------------------------
This is my gridview header. All are labels, now I want to find the label (october, noverber, self, officer..) how can I bind the data to them dynamically?
I have following code in gridview rowdatabound event.
foreach (GridViewRow gr in grdProducts.Rows)
{
if (e.Row.RowType == DataControlRowType.Header)
{
Label lM = (Label)gr.FindControl("lblMon1");
lM.Text = month1 + "-" + year1;
lM = (Label)gr.FindControl("lblMon2");
lM.Text = month2 + "-" + year2;
lM = (Label)gr.FindControl("lblMon3");
lM.Text = month3 + "-" + year3;
}
}
You can find it with below code snippet....
protected void gridview__RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
Label lblOctober = (Label)GridView1.FindControl("NameOflabelOctober");
lblOctober.Text = "What Ever you want to give value here(Same thing you can do for rest of four control.... ".
}
}
Check out this example.... @Surya sasidhar
In Aspx page.. I added below gridview
<asp:GridView ID="grvGrid" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CustomerID" OnRowDataBound="grvGrid_RowDataBound">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True"
SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName"
SortExpression="CompanyName" />
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lblMon1" runat="server"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblblbl" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
At code behind page, I added below code.....
protected void Page_Load(object sender, EventArgs e)
{
GetTable();
grvGrid.DataSource = dstable;
grvGrid.DataBind();
}
protected void grvGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
Label lblnothing = (Label)e.Row.FindControl("lblMon1");
lblnothing.Text = "November";
}
}
private void GetTable()
{
dstable.Columns.Add("CustomerID", typeof(int));
dstable.Columns.Add("CompanyName", typeof(string));
//
// Here we add five DataRows.
//
dstable.Rows.Add(25, "Indocin");
dstable.Rows.Add(50, "Enebrel");
dstable.Rows.Add(10, "Hydralazine");
dstable.Rows.Add(21, "Combivent");
dstable.Rows.Add(100, "Dilantin");
}
Here is the result....
精彩评论