GridView怎样修改列标题的值
protectedvoidPage_Load(objectsender,EventArgse){DataSetds=newDataSet();SqlConnectionc...
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=UserDB;Integrated Security=True");
string sql = "select QuestionTitle,QuestionClassify,State,AskTime from Question";
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
sda.Fill(ds);
this.GridView2.DataSource = ds.Tables[0];
this.GridView2.DataBind();
} 展开
{
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=UserDB;Integrated Security=True");
string sql = "select QuestionTitle,QuestionClassify,State,AskTime from Question";
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
sda.Fill(ds);
this.GridView2.DataSource = ds.Tables[0];
this.GridView2.DataBind();
} 展开
4个回答
推荐于2018-05-14 · 知道合伙人数码行家
可以叫我表哥
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:25897
获赞数:1464977
2010年毕业于北京化工大学北方学院计算机科学与技术专业毕业,学士学位,工程电子技术行业4年从业经验。
向TA提问 私信TA
关注
展开全部
修改列标题的值要先绑定,再用GridView.HeaderRow.Cells[n]
foreach (DataGridViewColumn col in this.dataGridView1.Columns)
{
string columnname = col.HeaderText;
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
//列数
int count = this.GridView1.HeaderRow.Cells.Count;
for (int i = 0; i < count; i++)
{
string oldValue = this.GridView1.HeaderRow.Cells[i].Text;
if (oldValue == "UserName")
{
this.GridView1.HeaderRow.Cells[i].Text = "用户名";
}
else if (oldValue == "Order")
{
this.GridView1.HeaderRow.Cells[i].Text = "订单";
}
else
{
this.GridView1.HeaderRow.Cells[i].Text = "其他";
}
}
}
foreach (DataGridViewColumn col in this.dataGridView1.Columns)
{
string columnname = col.HeaderText;
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
//列数
int count = this.GridView1.HeaderRow.Cells.Count;
for (int i = 0; i < count; i++)
{
string oldValue = this.GridView1.HeaderRow.Cells[i].Text;
if (oldValue == "UserName")
{
this.GridView1.HeaderRow.Cells[i].Text = "用户名";
}
else if (oldValue == "Order")
{
this.GridView1.HeaderRow.Cells[i].Text = "订单";
}
else
{
this.GridView1.HeaderRow.Cells[i].Text = "其他";
}
}
}
展开全部
你应该在aspx页面里面这样定义Gridview的模板
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="AdminID" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="Account" HeaderText="账号"
SortExpression="Account" />
<asp:BoundField DataField="Password" HeaderText="密码"
SortExpression="Password" />
</Columns>
</asp:GridView>
记住要把AutoGenerateColumns设为false,否则会自动绑定每一列
BoundField里面DataField就是字段名,HeaderText就是标题值
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="AdminID" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="Account" HeaderText="账号"
SortExpression="Account" />
<asp:BoundField DataField="Password" HeaderText="密码"
SortExpression="Password" />
</Columns>
</asp:GridView>
记住要把AutoGenerateColumns设为false,否则会自动绑定每一列
BoundField里面DataField就是字段名,HeaderText就是标题值
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
只需在调用DataBind()方法前,改变GridView对应Columns的HeaderText属性即可。例:
GirdView2.Columns[0].HeaderText = “aaa”;
注意,该语句必需出现在GirdView2.DataBind();之前,否则不起作用。
GirdView2.Columns[0].HeaderText = “aaa”;
注意,该语句必需出现在GirdView2.DataBind();之前,否则不起作用。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<html xmlns="http://www.w8.org/8999/xhtml" > <head runat="server"> <title>无标题页</title> ... <asp:GridView ID="GridView8" runat="server" AutoGenerateColumns="False" ...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询