what is the use of gridview rowdatabound event in asp.net c#

Posted by Prabu_Spark under ASP.NET on 4/22/2013 | Points: 10 | Views : 117778 | Status : [Member] | Replies : 6
Hi sir,
Tell me the use of gridview rowdatabound event in asp.net c#. I am new to dotnet field, tell me with sample code.
Kindly give me the solution for this problem.

With regards,
J.Prabu.
[Email:prbspark@gmail.com]



Responses

Posted by: Satyapriyanayak on: 4/22/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
http://msdn.microsoft.com/en-IN/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx
http://asp-net-example.blogspot.in/2008/11/aspnet-gridview-rowdatabound-event.html
aspnet-with-c-sharp.blogspot.com/2011/01/rowdatabound-event-of-gridview-of.html

If this post helps you mark it as answer
Thanks

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kmandapalli on: 4/23/2013 [Member] Silver | Points: 25

Up
0
Down
Hi,

Whenever we want to manipulate the appearance on some cell based on the value of that cell, for example displaying negative numbers in a different color, and so on, then we use RowDataBound Event.

Example:[/U
Write the following code in Default.aspx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
DataKeyNames="ProductId" onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="SetEno">
<ItemTemplate>
<asp:DropDownList ID="ddlEmpid" runat="server" Width="90%" DataSourceID="SqlDataSource1" DataTextField="ProductId"
DataValueField="ProductId" >
</asp:DropDownList>
</ItemTemplate>

</asp:TemplateField>
<asp:TemplateField HeaderText="ProductId" Visible="false">
<ItemTemplate>
<asp:Label ID="lblEmpNo" runat="server" Text='<%# Eval("ProductId") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProductId" HeaderText="ProductId" ReadOnly="true" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" />

</Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnection %>"
SelectCommand="SELECT [ProductId] FROM ProductsMvc"></asp:SqlDataSource>

Write the following code in Default.aspx.cs:

SqlConnection con;
protected void Page_Load(object sender, EventArgs e)
{
string conection;
conection = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString.ToString();
con = new SqlConnection(conection);
FillGrid();
}

protected void FillGrid()
{
SqlCommand cmd = new SqlCommand("select * from ProductsMvc", con);
con.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
con.Close();
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlEmpid");
Label lblEmpid = ((Label)e.Row.FindControl("lblEmpNo"));
e.Row.Cells[3].ForeColor = Color.Red;
string x = lblEmpid.Text.ToString().Trim();
ddl.SelectedValue = x;
}
}

You can modify the code in roeDataBound event according to your requirement.

Thanks,
Shree M.

Kavya Shree Mandapalli

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kmandapalli on: 4/23/2013 [Member] Silver | Points: 25

Up
0
Down
Hi,

Whenever we want to manipulate the appearance on some cell based on the value of that cell, for example displaying negative numbers in a different color, and so on, then we use RowDataBound Event.

Example:
Write the following code in Default.aspx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
DataKeyNames="ProductId" onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="SetEno">
<ItemTemplate>
<asp:DropDownList ID="ddlEmpid" runat="server" Width="90%" DataSourceID="SqlDataSource1" DataTextField="ProductId"
DataValueField="ProductId" >
</asp:DropDownList>
</ItemTemplate>

</asp:TemplateField>
<asp:TemplateField HeaderText="ProductId" Visible="false">
<ItemTemplate>
<asp:Label ID="lblEmpNo" runat="server" Text='<%# Eval("ProductId") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProductId" HeaderText="ProductId" ReadOnly="true" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" />

</Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnection %>"
SelectCommand="SELECT [ProductId] FROM ProductsMvc"></asp:SqlDataSource>

Write the following code in Default.aspx.cs:

SqlConnection con;
protected void Page_Load(object sender, EventArgs e)
{
string conection;
conection = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString.ToString();
con = new SqlConnection(conection);
FillGrid();
}

protected void FillGrid()
{
SqlCommand cmd = new SqlCommand("select * from ProductsMvc", con);
con.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
con.Close();
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlEmpid");
Label lblEmpid = ((Label)e.Row.FindControl("lblEmpNo"));
e.Row.Cells[3].ForeColor = Color.Red;
string x = lblEmpid.Text.ToString().Trim();
ddl.SelectedValue = x;
}
}

You can modify the code in roeDataBound event according to your requirement.

Thanks,
Shree M.

Kavya Shree Mandapalli

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hornstalk on: 11/18/2013 [Member] Starter | Points: 25

Up
0
Down
GridView data binding,
http://www.kettic.com/winforms_ui/csharp_guide/gridview_populate_data_binding.shtml

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: t5j9033387989 on: 11/29/2013 [Member] Starter | Points: 25

Up
0
Down
This event is calls when a data row is bound to data in a GridView control.

please read this msdn material it will gives detail description .

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound%28v=vs.110%29.aspx



mark this answer if it will really help you,

Thanks&Regards
ketan

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 11/29/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down


See the below link:-

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound(v=vs.110).aspx

If this helps you towards the solution, click on MARK IT AS ANSWER

Happy Coding.

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response