请教Gridview问题。
建立了一个gridview,其中一个表头列的标题想根据某一变量的值而设定,代码如下:GridView1.HeaderRow.Cells[12].Text = strdate1;
但是总提示Object reference not set to an instance of an object.
应该怎样动态设定表头内容。
谢谢 --------------------编程问答--------------------
--------------------编程问答-------------------- 你这样写是对的 你看看strdate1这个数据是什么 --------------------编程问答-------------------- strdate1数据没问题,取值8/1/2012。
protected void gvlist_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridViewRow row = this.gvlist.HeaderRow;
row = e.Row;
row.Cells[0].Text = "aaaaaaa";
}
}
To chinajiyong :您说的这个代码在哪里写啊,怎样触发。初学者,不懂。
我原来是有一个button_click点击后出现gridview. --------------------编程问答-------------------- gridview 找到事件里的 RowDataBound 双击 --------------------编程问答-------------------- 没找到,gridview没有事件,双击gridview,只出现index_change事件。 --------------------编程问答-------------------- dataGridView1.Columns["age"].HeaderText = "岁数";
你懂得
--------------------编程问答--------------------
gridview 找到事件里的 RowDataBound 双击 --------------------编程问答--------------------
属性-》点击闪电符号-》找RowDataBound事件,在框里双击 --------------------编程问答-------------------- 找到了,谢谢了。晕,咋又不能发帖子了,我的分哪里去了啊。
只好借这里在问个问题了
在给gridview某一列赋值时(等于另外两列的乘积),当其中一列值为null或者空时,总是出现数据类型错误。 即使我加了isempty判断,也是不行。不到下面代码有何问题?所有变量都在前面定义了,没有粘贴上来。
bool isEmpty = (GridView1.Rows[i].Cells[10].Text.ToString().Length == 0);--------------------编程问答-------------------- 顶。另祝大家新春快乐 --------------------编程问答--------------------
if (isEmpty)
{
GridView1.Rows[i].cells[10].text = "0";
}
GridView1.Rows[i].Cells[13].Text = (float.Parse(GridView1.Rows[i].Cells[10].Text) * float.Parse(GridView1.Rows[i].Cells[12].Text)).ToString("f2");
GridView1.Rows[i].Cells[15].Text = (float.Parse(GridView1.Rows[i].Cells[10].Text) * float.Parse(GridView1.Rows[i].Cells[14].Text)).ToString("f2");
GridView1.Rows[i].Cells[17].Text = (float.Parse(GridView1.Rows[i].Cells[10].Text) * float.Parse(GridView1.Rows[i].Cells[16].Text)).ToString("f2");
你可以把标题放到一个 protected string 的变量里边,然后在你的设计页面上将相应的列转为模板列,然后在vs设计器窗口上编辑这个列,为这个列的 HeaderTemplate 模版里边随便设计,图片、文字等都可以。比如其中拖入有一个Label控件,然后编辑其Text属性为
Text="<%# MycolumnTitle %>"这就OK了。 --------------------编程问答-------------------- 未将对象引用到对象实例 --------------------编程问答-------------------- 没对象
补充:.NET技术 , C#