asp.net(C#) 项目
小弟在DataGrid中添加了CheckBox,现在要实现这样的功能:如果该行的CheckBox被选中,那么返回该行第三个单元格中的内容。我的代码如下:
private void BnSelec_Click(object sender, System.EventArgs e)
{
CheckBox inChk;
string temp = "";
foreach (DataGridItem i in this.Dg_Client.Items)//Dg_Client为DataGrid的ID
{
inChk = (CheckBox)i.FindControl("checkbox");
if (inChk.Checked)
{
    temp =  i.Cells[2].Text;//问题就在这里
                                               //取得的值为空
}
}
Lable.Text = temp;
}
请高手帮忙解决一下,谢谢!!!学习,关注……

解决方案 »

  1.   

    temp =  i.Cells[2].Text;//问题就在这里
    -------------------------------------------
    跟踪过吗?这里能走到吗?如果没有走到,就是判断语句的问题.
      

  2.   

    for(int i=0;i<Dg_Client.Items.Count;i++)
    {
    CheckBox checkbox=(CheckBox)Dg_Client.Items[i].FindControl("checkbox");
    if(checkbox.Checked)
    {
    string temp=Dg_Client.Items[i].Cells[2].Text.ToString();
    }
    }
      

  3.   

    另外,button的click事件之前会触发Page的PostBack事件,这里如果没有重新绑定datagrid,datagrid数据会丢失.
      

  4.   

    private void BnSelec_Click(object sender, System.EventArgs e)
    {
    CheckBox inChk;
    string temp = "";
    for(int i=0;i<this.Dg_Client.Items.Count;i++)
    {
    inChk = (CheckBox)this.Dg_Client.Items[i].FindControl("复选框ID");
    if(inChk.Checked == true)
    {
    temp = this.Dg_Client.Items[i].Cell[2].Text;
    break;
    }
    } Lable.Text = temp;
    }
      

  5.   

    对,第三列是否为摸班列??我试了一下 hchxxzx(NET?摸到一点门槛) 大哥的方法也是空值
    用了绑定列的话就可以了!!
      

  6.   

    private void BnSelec_Click(object sender, System.EventArgs e)
    {
    CheckBox inChk;
    string temp = "";
    foreach (DataGridItem i in this.Dg_Client.Items)
    {
    inChk = (CheckBox)i.Item.FindControl("CheckBox");
    if (inChk.Checked)
    {
        temp =  i.Item[i].Cells[2].Text;
                       }
    }
    Lable.Text = temp;
    }
    这样就可以了!!
      

  7.   

    i.Item[i].Cells[2].Text;这行代码应该是不对的吧:
    1、i是DataGridItem的对象,没有Item[]这个属性
    2、Item[i]这个里面的i是什么意思啊?不懂!!!学习,关注……
      

  8.   

    to:hackate(兰花开香入梦境,独思佳人亦飘然!!)
    那正确的是怎么样啊??
      

  9.   

    有时候,绑定的方式不同,产生的结果也不同
    你可试用
    this.Label1.Text += this.Dg_Client.Items[i].Cell[2].Controls.Count.ToString();
    this.Label1.Text += this.Dg_Client.Items[i].Cell[2].Controls[0].GetType().ToString();
    看看它到底有几个控件,和第一个控件什么类型,有时候,它的文字会在Cell[2]的下属控件里面