如何在DataGrid中点击任意一行实现此行的选取功能?

解决方案 »

  1.   

    <asp:ButtonColumn Text="选择" CommandName="Select"></asp:ButtonColumn>
      

  2.   

    加选择按钮列(CommandName="Select"),将其隐藏
    在OnPreRender(..)
    里添加
    foreach(DataGridItem dgi in yourDataGrid.Items)
    {
    int intII = dgi.ItemIndex + 2 ;
    string str = "__doPostBack('yourDataGrid$_ctl"+intII.ToString()
    +"$_ctl0','')";
    dgi.Attributes.Add("onclick",str);
    }
      

  3.   

    如果是要单击某行的任意位置的话就达到选取这行的目的的话,可以放在ItemBound事件中执行。
    private void DataGrid1_ItemDataBound_1(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
    {
    e.Item.Attributes.Add("onclick","javascript:ChangeRowColor(this)");
    e.Item.Attributes.Add("ondblclick","javascript:getData(this,"+e.Item.Cells[0].Text+")");
    }
    }前台的javascriptvar oldRow="";
    function ChangeRowColor(rowObj)
    {
    if(oldRow=="")
    {
    oldRow=rowObj;
    oldRow.style.backgroundColor='white';
    rowObj.style.backgroundColor='#FEE6A7';
    }
    if(oldRow!=rowObj)
    {
    oldRow.style.backgroundColor='white';
    rowObj.style.backgroundColor='#FEE6A7';
    oldRow=rowObj;
    }
    }

    function getData(rowObj,ID)
    {
    ChangeRowColor(rowObj);
    var getControl=window.showModalDialog("HepatitisCheckCardView_p.aspx",ID,"dialogWidth=800px;dialogHeight=300px");
    }
      

  4.   

    多谢 rongjf(橡皮鸟) 大侠,此方法果然管用