I have one gridview in one form and I have use template inside grid. In the template I have used the labels.
Problem is I want to bind the data which I have extracted from Sql table to that label.
Please help me here. I have to submit this today. I am helpless, tried all the options.
Please tell me how I do this
SOLUTION 1:
You can do it in 2 ways:
1. In the aspx page set the Text property of the label to “Text = ‘<% Eval(<field name>) %>’ “ The <field Name> is the name of the field as returned from the database.
2. For run time binding you can use the gridviews RowDataBound or DataBound event.
- The second parameter of the RowDataBound event contains the row index of the current row with which the label control is found and text property can be assigned. This event fires when the data is bound to each row.
- If you use DataBound event then you have to manually iterate through each row in the grid view and find the label control and assign the text property.
Hope this answers the question.
SOLUTION 2:
I have worked on the similar problem when I was in college! All you have to do is use FindControl() to find your label in gridview and the bind data to it. Following link will make this approch more clearer to you.
SOLUTION 3:
See the code below where in StartPoint is the column name of sql table.
Generally TemplateField has to be used when you want to get the data indirectly, when data is coming directly from database BoundField should be used.
<asp:TemplateField HeaderText="From">
<ItemTemplate>
<asp:Label ID="lblFromLocation" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.StartPoint") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Wrap="False" Font-Size="10pt" />
</asp:TemplateField>
You can also assign value dynamically in RowDataBound Event:
protected void gvManagePoolMembers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblReqStatus = (Label)e.Row.FindControl("lblMemberStatus");
if (lblReqStatus.Text == "0")
{
lblReqStatus.Text = "Pending";
// e.Row.Cells[4].Text = "Pending";
}
}
}
This should solve your problem.
No comments:
Post a Comment