如何判断DataGrid里的某一行被选了?

解决方案 »

  1.   

    DataGrid.CurrentRowIndex
    获取或设置选定行的索引
      

  2.   

    我写的一个:在这个函数里:
    private void MyDataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    //////////////////配合前台脚本实现鼠标移动到每行上变颜色和点击没列的头上是会变颜色即而实现隐藏列
    e.Item.ID="dg"+e.Item.Cells[0].Text.ToString();
    if(e.Item.ItemIndex>=0)
    {
    e.Item.Attributes.Add("onmouseover","ItemSet(this)");//////在每行上增加脚本处理
    }
    else
    {
    for(int i=0;i<e.Item.Cells.Count;i++)
    {
    e.Item.Cells[i].ID="dg"+i;
    e.Item.Cells[i].Attributes.Add("onclick","CellsSet(this,'"+(i+1)+"')");//////在每列的头上增加脚本处理
    }
    }
    /////////////////////
    }前台的脚本:
    <script language="javascript">
    if (!objbeforeItem)
    {
    var objbeforeItem=null;
    var objbeforeItembackgroundColor=null;
    }
    function ItemSet(obj)
    {
    if(objbeforeItem)
    {
    objbeforeItem.style.backgroundColor=objbeforeItembackgroundColor;
    }
    objbeforeItembackgroundColor=obj.style.backgroundColor;
    obj.style.backgroundColor="#92d4fe";
    objbeforeItem=obj;
    }if (!objbefore)
    {
    var objbefore=null;
    }
    function CellsSet(obj,cellId)
    {
    if(objbefore)
    {
    objbefore.style.backgroundColor="";
    }
    obj.style.backgroundColor="#92d4fe";
    objbefore=obj; }
    </script>结合脚本,当鼠标移动到一行上时,这一行就变颜色,另外点击头行的列时也变颜色,
    也可以在脚本中写自己的处理
      

  3.   

    如果是Windows Form下。
    or(int i = 0;i < dataGrid1.VisibleRowCount;i++)
    {
    if(this.dataGrid1.IsSelected(i))
    {
    //选中
    }
    }
      

  4.   

    http://community.csdn.net/Expert/topic/3896/3896149.xml?temp=.5940821