我正在从Server检索数据到ASP.Net中的网格视图。下面是从数据库中检索数据后的网格视图的标题。
Time| Atlanta_1| Atlanta_2| Atlanta_3|我想用“城市”代替城市的名字。
Time| City_1| City_2| City_3|因此,基本上,我想用"City“替换标题的一部分,并希望在将其绑定到网格视图(而不是Server中)中时这样做。下面是如何从数据库检索数据到网格视图的代码
SqlConnection con = new SqlConnection("My Connection");
string s = "My Stored Procedure";
con.Open();
SqlDataAdapter da = new SqlDataAdapter(s, con);
DataSet ds = new DataSet();
da.Fill(ds);
gridView1.DataSource = ds;
gridView1.DataBind();
con.Close();发布于 2018-08-07 19:05:18
您可以使用RowDataBound事件。然后循环所有标头单元格。检查_的单元格并用正确的值替换。如果总是Atlanta,您只需做一个替换。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (TableCell cell in e.Row.Cells)
{
if (cell.Text.Contains("_"))
{
cell.Text = "City_" + cell.Text.Split('_')[1];
}
}
}
}https://stackoverflow.com/questions/51733126
复制相似问题