如何改变gridview的宽度和绑定数据后重新命名的标题 [英] How to change the gridview width and rename the header after binding data

查看:148
本文介绍了如何改变gridview的宽度和绑定数据后重新命名的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变GridView的大小和绑定数据后重新命名的标题,但我得到了错误索引超出范围,因为我的数据都超过100像素,怎么解决呢?

  conn.Open();
                cmdString =SQL;
                CMD =新的SqlCommand(cmdString,康涅狄格州);
                cmd.Parameters.AddWithValue(@ courseID,CourseID);
                DTR = cmd.ExecuteReader();
                gvAssignment.DataSource = DTR;
                gvAssignment.Columns [7] .ItemStyle.Width = Unit.Pixel(100);
                gvAssignment.DataBind();
                dtr.Close();
                conn.Close();

另一种方法错误,但规模不会更改为100

 保护无效gvAssignment_RowDataBound(对象发件人,GridViewRowEventArgs E)
    {
        e.Row.Cells [7] .WIDTH = 100;
    }


解决方案

首先尝试设置的DataBind,就像如下:

  gvAssignment.DataBind();
   gvAssignment.Columns [7] .ItemStyle.Width = Unit.Pixel(100);

您也可以尝试更新列宽中的数据已经绑定了。为了确保这一点,我们可以将其设置在GridView绑定的事件中

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    gvAssignment.DataBound + =新的EventHandler(gvAssignment_DataBound);
}无效gvAssignment_DataBound(对象发件人,EventArgs的发送)
{
    gvAssignment.Columns [7] .ItemStyle.Width = Unit.Pixel(100);
}

i try to change the gridview size and rename the header after binding the data,but i got the error "Index was out of range" because my data are more than 100px,How to solve it?

                conn.Open();
                cmdString = "SQL";
                cmd = new SqlCommand(cmdString, conn);
                cmd.Parameters.AddWithValue("@courseID", CourseID);
                dtr = cmd.ExecuteReader();
                gvAssignment.DataSource = dtr;
                gvAssignment.Columns[7].ItemStyle.Width = Unit.Pixel(100);
                gvAssignment.DataBind();                  
                dtr.Close();
                conn.Close();

another method no error but the size not change to 100

    protected void gvAssignment_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[7].Width = 100;
    }

解决方案

Try setting the DataBind first, like follows:

   gvAssignment.DataBind();  
   gvAssignment.Columns[7].ItemStyle.Width = Unit.Pixel(100); 

You can also try updating the column width after the data has been bound. To ensure this, we can set it inside the gridview bound event

protected void Page_Load(object sender, EventArgs e)
{
    gvAssignment.DataBound += new EventHandler(gvAssignment_DataBound);
}

void gvAssignment_DataBound(object sender, EventArgs e)
{       
    gvAssignment.Columns[7].ItemStyle.Width = Unit.Pixel(100); 
}   

这篇关于如何改变gridview的宽度和绑定数据后重新命名的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆