how would i change the value IBM to something arbitrary like Cisco in one of the gridview events listed?
there can be varying columns in the dynamic gridview so would be nice to address the column by name.
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable("TestTable");
dt.Columns.AddRange(new DataColumn[] { new DataColumn("id"), new DataColumn("customername") });
DataRow dr = dt.NewRow();
dr[0] = "1";
dr[1] = "Microsoft";
dt.Rows.Add(dr);
DataRow dr2 = dt.NewRow();
dr2[0] = "2";
dr2[1] = "IBM";
dt.Rows.Add(dr2);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_DataBinding(object sender, EventArgs e)
{
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
}
}
}
This won’t work when AutogenerateColumns is set to
true(default). You need to add the columns programmatically or declaratively(in aspx markup). Then you can use a TemplateField with a Control likeLabelthat you can reference in codebehind:For example:
RowDataBound is perfect(for almost everything):
Edit: Ok, never needed to do such. But actually you can change the Microsoft values to Cisco even with
AutoGenerateColumnsset totrue.DataBinding event is triggered before the
GridViewis databound. If you change the datasource before it’s bound to grid, you’ll be able to modify it:Note: of course you can also use a simple loop instead of LINQ