在线等,马上结贴:(web) 当用鼠标 Click datagrid(web) 的某一行 怎么样触发一个事件,并获得该行的Index? 3Q !!!

解决方案 »

  1.   

    http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q689q5.11 How can I select the entire row when the user clicks on a cell in the row?     Call the DataGrid.Select method from within its mouseup event. 
     
    [C#] 
     
    private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) 
     

     
         System.Drawing.Point pt = new Point(e.X, e.Y); 
     
         DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt); 
     
         if(hti.Type == DataGrid.HitTestType.Cell) 
     
         { 
     
              dataGrid1.CurrentCell = new DataGridCell(hti.Row, hti.Column); 
     
              dataGrid1.Select(hti.Row); 
     
         } 
     

      
    [VB/NET] 
     
    Private Sub dataGrid1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dataGrid1.MouseUp 
     
         Dim pt = New Point(e.X, e.Y) 
     
         Dim hti As DataGrid.HitTestInfo = dataGrid1.HitTest(pt) 
     
         If hti.Type = DataGrid.HitTestType.Cell Then 
     
              dataGrid1.CurrentCell = New DataGridCell(hti.Row, hti.Column) 
     
              dataGrid1.Select(hti.Row) 
     
         End If 
     
    End Sub
      

  2.   

    参考:
    http://blog.blogchina.com/websites/1038449.html
      

  3.   

    http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q785q5.31 How do I get the row or column that has been clicked on?     You can use the DataGrid's HitTest method, passing it a point in the grid's client coordinate system, and returning a HitTestInfo object that holds all the row and column information that you want. 
     
    [C#] 
     
    // X & Y are in the grid' coordinates. If they are in screen coordinates, call dataGrid1.PointToClient method 
     
    System.Drawing.Point pt = new Point(X, Y); 
     
    DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt); 
     
    if(hti.Type == DataGrid.HitTestType.Cell) 
     

     
         MessageBox.Show(dataGrid1[hti.Row, hti.Column].ToString()); 
     

     
    else if(hti.Type == DataGrid.HitTestType.ColumnHeader) 
     

     
         MessageBox.Show(((DataView) DataGrid1.DataSource).Table.Columns[hti.Column].ToString()); 
     

      
    [VB.NET] 
     
    ' X & Y are in the grid' coordinates. If they are in screen coordinates, call dataGrid1.PointToClient method 
     
    Dim pt = New Point(X, Y) 
     
    Dim hti As DataGrid.HitTestInfo = dataGrid1.HitTest(pt) 
     
    If hti.Type = DataGrid.HitTestType.Cell Then 
     
         MessageBox.Show(dataGrid1(hti.Row, hti.Column).ToString()) 
     
    Else 
     
         If hti.Type = DataGrid.HitTestType.ColumnHeader Then 'assumes datasource is a dataview 
     
              MessageBox.Show(CType(DataGrid1.DataSource, DataView).Table.Columns(hti.Column).ToString()) 
     
         End If 
     
    End If
     
      

  4.   

    5.46 How do I catch a doubleclick in my datagrid?
    http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q869q
      

  5.   

    楼上  LaoDai_Net(『老代.Net』)  哥哥:那个网站的代码不全阿,能否给个例子?
      

  6.   

    如果是点击按钮列就比较好整了.直接在itemcommand处理程序中获取就行了.
    void handleCommands(object sender,datagridCommandeventargs e)
    {
    e.item.itemindex;
    }
      

  7.   

    还是使用客户端方法吧,否则会影响效率的.
    在DataGrid的ItemDataBound方法里加上一句:
    e.Item.Attributes.Add("ID",e.Item.ItemIndex.ToString())
    e.Item.Attributes.Add("onclick","javascript:return TRClick(this.id)");
    客户端脚本
    <script language=javascript>
       function TRClick(value)
       {
            alert(value);
       }
    </script>
      

  8.   

    忘了补上一句,当e.Item.ItemIndex为-1时,表示该行为表头.
      

  9.   

    Jimyu1110(Jim Yu) :了解。
    可我要处理很烦的逻辑,html 不好写的
      

  10.   

    <a href='#' onclick="javascript:setValue(this)><%# DataBinder.Eval(Container.DataItem,"id") %>
    </a>
    <script language="javascript">
    function setValue(strValue)
    {
    window.open("type.aspx?id="+strValue.innerText);}
    </script>
      

  11.   

    如果你确实要放在服务器端去做的话,不妨在你单击该行时把该行的ID写到一个隐藏的TextBox里,然后把这个TextBox回传到服务器中,在TextBox里的TextChanged事件里去做.
    承上:
    在客户端里加一个TextBox: 
    <asp:TextBox ID=Text Runat=server style="display:none"></asp:TextBox>
    客户端脚本
    <script language=javascript>
       function TRClick(value)
       {
            document.all("Test",value);
            __doPostBack("Test","");
       }
    </script>
      

  12.   

    这段时间睡眠不足老是忘了提醒:
    在TextBox里的TextChanged要注意在使用完TextBox的内容时一定要把它清空,否则如果再次单击这一行时因为内容相同将不会提交的.
    同时如果在客户端脚本中出现找到不到__doPostBack这个方法的话,可以把刚才的TextBox的定义改改为:
    <asp:TextBox ID=Text Runat=server style="display:none" AutoPostBack=True ></asp:TextBox>
    系统就会自动给你生成__doPostBack这个方法的了(__为两个下划线).