关于GridView中如何动态绑定数据 关于表头为汉语的

刚毕业 公司出了个小题目让做 其中涉及到GridView的动态绑定数据。

不过如果想让表头中显示汉字的话,就需要你再在数据表中添加一个专门的汉字字段了 假设他就是第二列字段

代码如下:

string connection = ConfigurationManager.ConnectionStrings["connection"].ToString();

protected void Page_Load(object sender, EventArgs e)
        {

               SqlConnection con = new SqlConnection(connection);
                con.Open();

               SqlDataAdapter sda = new SqlDataAdapter(“select语句”,con);

                DataSet ds = new DataSet();
                sda.Fill(ds);

             string[] names = new string[ds.Tables[0].Rows.Count];//声明数组用来存放中文名

            for (int m = 0; m < ds.Tables[0].Rows.Count; m++)
                {

                    names[m]=ds.Tables[0].Rows[i][1].ToString();
                    BoundField bf = new BoundField();
                    bf.HeaderText = names[m].ToString();
                    GridView1.Columns.Add(bf);
                }

              GridView1.DataSource = ss.Tables[0].DefaultView;
                GridView1.DataBind();

       }

如果你还想在 GridView1里再重新绑定一个列表 一定要记得在for前写上

          GridView2.Columns.Clear();//将原始绑定列清空

谢谢 希望高手能给出优化方案 呵呵

posted @ 2011-07-20 18:38  幻影NOr1  阅读(472)  评论(0)    收藏  举报