1.创建了一个DataGrid,并用绑定列显示出来.其中有一个列(年龄)的值是空值.
2.创建了一个object,怎样把这个object填充到年龄列里面?
ps:DataGrid里面有多条记录.

解决方案 »

  1.   

    for(int i = 0 ,i<DataGrid.Items.count,i++)
    {
    DataGrid.Items[i].cells[index].Text=object.ToString();//index是你的需要赋值列的索引值
    }
      

  2.   

    不知道什么意思。
    如果想添加一系列自己定的值,直接加到DataSet里,再绑定。
      

  3.   

    还是不行哦.代码如下:
    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”,因为它受保护级别限制
      

  4.   

    刚才原来我打错字了,cell[6]应该为Cell[6],大小写敏感.
    现在运行没出错,可惜没效果
      

  5.   

    获取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);
    }
      

  6.   

    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();
                }
            }
    试试看