DataGrid数据绑定的问题,比较特殊,请帮忙. 1.创建了一个DataGrid,并用绑定列显示出来.其中有一个列(年龄)的值是空值.2.创建了一个object,怎样把这个object填充到年龄列里面?ps:DataGrid里面有多条记录. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 for(int i = 0 ,i<DataGrid.Items.count,i++){DataGrid.Items[i].cells[index].Text=object.ToString();//index是你的需要赋值列的索引值} 不知道什么意思。如果想添加一系列自己定的值,直接加到DataSet里,再绑定。 还是不行哦.代码如下:private void myDataGrid_ItemDataBound(object sender,System.Web.UI.WebControls.DataGridItemEventArgs e){for(int i = 0;i < myDataGrid.Items.Count;i++){myDataGrid.Items[i].cells[6].Text=GetAge().ToString();//index是你的需要赋值列的索引值}}显示:myDataGrid.Items[i].cells[6].Text=GetAge().ToString()出错_______________________________________________________________不可访问“System.Web.UI.WebControls.TableRow.cells”,因为它受保护级别限制 刚才原来我打错字了,cell[6]应该为Cell[6],大小写敏感.现在运行没出错,可惜没效果 获取GetAge()的对象代码如下,没错吧?private object GetAge(){SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);string sqlstr = "select datediff(year,birthdate,getdate()) - case when dateadd(year,datediff(year,birthdate,getdate()),birthdate)>getdate() then 1 else 0 end from employees";SqlCommand myCommand = new SqlCommand(sqlstr,myConnection);myConnection.Open();object objAge = myCommand.ExecuteScalar();myConnection.Close();return(objAge);} protected void myDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { e.Item.Cells[6].Text = GetAge().ToString(); } }试试看 怎样通过隐藏域传值,急,在线等 HttpUtility.HtmlDecode难题 怎么合并数据表中的数据项 求高手解决 SQL 题目 关于08移动窗体模板的问题 顶者有分 关于水晶报表的问题,急! 高价找.net高手(石家庄)! nunit怎样集成到VS。NET中,该如何设置? 分页控件得不到图片。。 linux下运行asp.net问题。 问个url的问题 System.IO.IOException: 磁盘空间不足
{
DataGrid.Items[i].cells[index].Text=object.ToString();//index是你的需要赋值列的索引值
}
如果想添加一系列自己定的值,直接加到DataSet里,再绑定。
private void myDataGrid_ItemDataBound(object sender,System.Web.UI.WebControls.DataGridItemEventArgs e)
{
for(int i = 0;i < myDataGrid.Items.Count;i++)
{
myDataGrid.Items[i].cells[6].Text=GetAge().ToString();//index是你的需要赋值列的索引值
}
}
显示:
myDataGrid.Items[i].cells[6].Text=GetAge().ToString()出错
_______________________________________________________________
不可访问“System.Web.UI.WebControls.TableRow.cells”,因为它受保护级别限制
现在运行没出错,可惜没效果
{
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
string sqlstr = "select datediff(year,birthdate,getdate()) - case when dateadd(year,datediff(year,birthdate,getdate()),birthdate)>getdate() then 1 else 0 end from employees";
SqlCommand myCommand = new SqlCommand(sqlstr,myConnection);
myConnection.Open();
object objAge = myCommand.ExecuteScalar();
myConnection.Close();
return(objAge);
}
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Cells[6].Text = GetAge().ToString();
}
}
试试看