如何双击DataGrid的某行就能得到当前行的数据在网上查了很久,都是说在Web上的应用
我所需要的是windows程序上的请各位大哥大姐帮帮忙

解决方案 »

  1.   

    private void dgSongs_DoubleClick(object sender, System.EventArgs e)
    {
    string temp = "";
    dsSongs.Tables["Songs"].Rows[dgSongs.CurrentCell.RowNumber]["name"].ToString().Trim();
    MessageBox.Show(temp);
    }
      

  2.   

    DoubleClick事件下使用CurrentCell.Row属性取的当前行。dataGrid1[CurrentCell.Row,0]去当前行的第1个单元格的值!!!
      

  3.   

    我指的是双击此行的任何地方
    我发现好像只有行头和Cell间才可以响应双击事件
    难到双击cell就不能响应事件了吗?
    另外最好是双击后此行能被全部选种在线等待中......
      

  4.   

    用mousedown事件和hittest方法就可以了。
    mousedown得到下x,y坐标,通过hittest得到hittestinfo,可以通过hittestinfo的column,row属性的到行号,列号,你就可以对那列进行操作了。
      

  5.   

    双击里加上:
    dataGrid1.Select(dataGrid1.CurrentCell.RowNumber);
      

  6.   

    用mousedown事件和hittest方法就可以了。
    mousedown得到下x,y坐标,通过hittest得到hittestinfo,可以通过hittestinfo的column,row属性的到行号,列号,你就可以对那列进行操作了。
      
    强烈赞同!
      

  7.   

    private void dataGrid1_Click(object sender, System.EventArgs e)
    {
    System.Windows.Forms.DataGrid.HitTestInfo myHitTest;      
    myHitTest = dataGrid1.HitTest(Cursor.Position.X,Cursor.Position.Y);
    string strMessage ="自定义格式DataGrid\r\n";
    strMessage +=Display(myHitTest);
    textBox1.Text =strMessage;
    }private string Display(DataGrid.HitTestInfo myHitTest)
    {
    string strMessage ="";
    switch (myHitTest.Type)
    {
    case DataGrid.HitTestType.Cell:
    strMessage += "\t单元格 Column:" + myHitTest.Column.ToString() + " Row:" + myHitTest.Row.ToString();
    break;
    case DataGrid.HitTestType.Caption:
    strMessage += "\t标题";
    break;
    case DataGrid.HitTestType.ColumnHeader:
    strMessage += "\t列标题";
    break;
    case DataGrid.HitTestType.ColumnResize:
    strMessage += "\t列边框,现在可以拖动调整列宽";
    break;
    case DataGrid.HitTestType.None:
    strMessage += "\t背景区域";
    break;
    case DataGrid.HitTestType.RowHeader:
    strMessage += "\t行标题";
    break;
    case DataGrid.HitTestType.RowResize:
    strMessage += "\t行边框,现在可以拖动调整行高";
    break;
    }
    return strMessage;
    }
      

  8.   

    多谢楼上几位朋友的大力支持
    但我现在的问题不是取其中的数据了我最大的问题是这样在Cell上响应鼠标事件
    看样子我的想法是条死路,根本就不可能实现
      

  9.   

    http://dotnet.aspx.cc/ShowDetail.aspx?id=C83C3A4B-8571-4CE6-FBAC-35DC28D14389
      

  10.   

    dataGrid1[dataGrid1.CurrentCell.Row,0]  这样绝对可以!
      

  11.   

    你需要先重写DataGrid的DataGridTableStyle,如DataGridTableStyle ts1 = new DataGridTableStyle();
    ts1.MappingName = "Customers";DataGridTextBoxColumn TextCol = new DataGridTextBoxColumn();
    TextCol.MappingName = "custID";
    TextCol.HeaderText = "CustomerID";
    TextCol.Width = 100;
    //add handler
    TextCol.TextBox.MouseDown += new MouseEventHandler(TextBoxMouseDownHandler);
    TextCol.TextBox.DoubleClick += new EventHandler(TextBoxDoubleClickHandler);
    ts1.GridColumnStyles.Add(TextCol);
    //Other Columns HeredataGrid1.TableStyles.Add(ts1);下面是两个Handler
    private void TextBoxDoubleClickHandler(object sender, EventArgs e)
    {
    MessageBox.Show("TrueDoubleClick");
    }
    private void TextBoxMouseDownHandler(object sender, MouseEventArgs e)
    {
    if(DateTime.Now < gridMouseDownTime.AddMilliseconds(SystemInformation.DoubleClickTime))
    {
    MessageBox.Show("GridDoubleClick");
    }
    Console.WriteLine("TextBoxMouseDownHandler  " );
    }
    给DataGrid添加MouseDown事件
    private void dataGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    gridMouseDownTime = DateTime.Now;
    Console.WriteLine("dataGrid1_MouseDown  " );
    }其中gridMouseDownTime 是DateTime类型的,并且在Form初始化时进行赋值
    public Form1()
    {
    InitializeComponent();
    gridMouseDownTime = DateTime.Now;
    }
      

  12.   

    愚认为以上的方法在不排序下可行,如果你单击了列头的排序后,可能不行。 lampson123(微软) 的说的对。
      

  13.   

    ffjing(卡卡)的方法我试了仍然不行
      

  14.   

    Grid有个双击的方法,用来引发事件就可以了。
    或者两个数据表做一个Relation。
      

  15.   

    CurrentChange事件 的单击取数据,怎么不用?要用双击呢?
      

  16.   

    我想做的是双击弹出此条信息,单击仅仅是选中,这样才更符合windows的使用习惯啊
      

  17.   

    这是我写的一个继承DataGrid的类,实现单行选择和整行选择显示,也许对你有用。
    public class MyDataGrid:DataGrid
    {
    public MyDataGrid()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }
    private int oldSelectedRow = 0;
    // public Point pointInCell00; protected override void OnMouseDown(MouseEventArgs e)
    {
    if(e.Button == MouseButtons.Left) 
    {
    System.Drawing.Point pt = new Point(e.X, e.Y); 
    DataGrid.HitTestInfo hti = this.HitTest(pt); 
    if(hti.Type == DataGrid.HitTestType.Cell) 

    this.CurrentCell = new DataGridCell(hti.Row, hti.Column);
    this.UnSelectAll();
    this.Select(hti.Row);
    oldSelectedRow = hti.Row;
    canClick = true;
    }
    else if(hti.Type == DataGrid.HitTestType.RowHeader) 
    {
    // if(oldSelectedRow > -1)
    // this.UnSelect(oldSelectedRow);
    // if((Control.ModifierKeys & Keys.Shift) == 0)
    // base.OnMouseDown(e);
    // else
    // this.CurrentCell = new DataGridCell(hti.Row, hti.Column);
    this.UnSelectAll();
    this.Select(hti.Row);
    oldSelectedRow = hti.Row;
    }
    else 
    {
        base.OnMouseDown(e);
    this.UnSelectAll();
    if(this.VisibleRowCount > 0)
    this.Select(oldSelectedRow);
    }
    }
    base.OnMouseDown (e);
    }

    protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e) 

    if(e.Button == MouseButtons.Left) {
    System.Drawing.Point pt = new Point(e.X, e.Y); 
    DataGrid.HitTestInfo hti = this.HitTest(pt); 
    if(hti.Type == DataGrid.HitTestType.Cell) { 
    this.CurrentCell = new DataGridCell(hti.Row, hti.Column);
    this.UnSelectAll();
    this.Select(hti.Row);
    oldSelectedRow = hti.Row;
    }
    else if(hti.Type == DataGrid.HitTestType.RowHeader) {
    // if(oldSelectedRow > -1)
    // this.UnSelect(oldSelectedRow);
    // if((Control.ModifierKeys & Keys.Shift) == 0)
    // base.OnMouseDown(e);
    // else
    // this.CurrentCell = new DataGridCell(hti.Row, hti.Column);
    this.UnSelectAll();
    this.Select(hti.Row);
    oldSelectedRow = hti.Row;
    }
    else {
    base.OnMouseUp(e);
    this.UnSelectAll();
    // if(oldSelectedRow>-1)
    if(this.VisibleRowCount>0)
    this.Select(oldSelectedRow);
    }
    }
    }  public void UnSelectAll() {
    for(int i=0;i<this.VisibleRowCount;i++)
    this.UnSelect(i);
    } /// <summary>
    /// 移动水平滚动条时产生的事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="se"></param>
    protected override void GridHScrolled(object sender,ScrollEventArgs se)
    {
    base.GridHScrolled(sender,se);
    if(oldSelectedRow>-1)
    this.Select(oldSelectedRow);
    }
    }
      

  18.   

    private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    Point pt = new Point(e.X,e.Y);
    DataGrid.HitTestInfo hit = dataGrid1.HitTest(pt);
    if(hit.Type == DataGrid.HitTestType.Cell) 
    {
    dataGrid1.Select(hit.Row); 
    }
    }
      

  19.   

    上面的时间处理函数发错了,看
    http://dotnet.aspx.cc/ShowDetail.aspx?id=C83C3A4B-8571-4CE6-FBAC-35DC28D14389
      

  20.   

    居然还没解决,搞不懂你是怎么试的,直接去下事例吧
    http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q1075q
      

  21.   

    可以用当前管理器来实现。
    //申名窗口局部变量
    private CurrencyManager cm_row;//在窗口加载事件中也就是dataGrid1.DataSource数据源赋值后写如下代码
    cm_row =(CurrencyManager) BindingContext[dataGrid1.DataSource];
    //在dataGrid1DoubleClick
    private void dataGrid1_DoubleClick(object sender, System.EventArgs e)
    {
        DataRowView dvrow = cm_row.Current as DataRowView;//双击后数据源的当前行
    }
      

  22.   

    说一下我的方法,首先声明变量 
    private int RowNum;
    private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
    {
    RowNum=dataGrid1.CurrentRowIndex;
    }
    private void dataGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
                               RowNum=dataGrid1.CurrentRowIndex;

    }
    private void dataGrid1_DoubleClick(object sender, System.EventArgs e)
    {
                               string YouOneColValue=dataGrid1[RowNum,0].ToString();
                               string YouTwoColValue=dataGrid1[RowNum,1].ToString();
                               ...................................................

    }
      

  23.   

    谢谢各位的大力支持,问题已经解决If you have added a TableStyle for your grid, then the code below should set the right column width to be the empty space from a button click. If you need to dynamically do this in response to the user sizing other columns, then there may be more work. But if you only need to do it at the end of your Form_Load, then this code might be sufficient. It assumes your datasource is a datatable. You can download a sample project (C#, VB).
    private void button1_Click(object sender, System.EventArgs e)
    {
         int numCols = ((DataTable)(dataGrid1.DataSource)).Columns.Count;     //the fudge -4 is for the grid borders
         int targetWidth = dataGrid1.ClientSize.Width - SystemInformation.VerticalScrollBarWidth - 4;     int runningWidthUsed = this.dataGrid1.TableStyles["customers"].RowHeaderWidth;
                   
         for(int i = 0; i < numCols - 1; ++i)
              runningWidthUsed += this.dataGrid1.TableStyles["customers"].GridColumnStyles[i].Width;     if(runningWidthUsed < targetWidth)
              this.dataGrid1.TableStyles["customers"].GridColumnStyles[numCols - 1].Width = targetWidth - runningWidthUsed;
    }===================================
    全部代码如下
    ===================================namespace DataGridDoubleClick
    {
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data; /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.DataGrid dataGrid1;
    private DataSet myDataSet;
    DateTime gridMouseDownTime;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent(); gridMouseDownTime = DateTime.Now;
    // Call SetUp to bind the controls.
    SetUp();
    } private void SetUp()
    {
    // Create a DataSet with two tables and one relation.
    MakeDataSet();
    /* Bind the DataGrid to the DataSet. The dataMember
    specifies that the Customers table should be displayed.*/
    dataGrid1.SetDataBinding(myDataSet, "Customers"); //create and add a custom table style so we can
    //easily get at the behavior of a cell...
    AddCustomDataTableStyle();
    } private void MakeDataSet()
    {
    // Create a DataSet.
    myDataSet = new DataSet("myDataSet");
          
    // Create two DataTables.
    DataTable tCust = new DataTable("Customers");

    // Create two columns, and add them to the first table.
    DataColumn cCustID = new DataColumn("custID");
    DataColumn cCustName = new DataColumn("custName");
    DataColumn cCurrent = new DataColumn("custCity");
    tCust.Columns.Add(cCustID);
    tCust.Columns.Add(cCustName);
    tCust.Columns.Add(cCurrent); // Add the tables to the DataSet.
    myDataSet.Tables.Add(tCust);

       
    /* Populates the tables. For each customer and order, 
    creates two DataRow variables. */
    DataRow newRow1;

    // Create three customers in the Customers Table.
    for(int i = 1; i < 4; i++)
    {
    newRow1 = tCust.NewRow();
    newRow1["custID"] = (100*i).ToString();
    tCust.Rows.Add(newRow1);
    }
    // Give each customer a distinct name.
    tCust.Rows[0]["custName"] = "John Summers";
    tCust.Rows[1]["custName"] = "Phil Seagram";
    tCust.Rows[2]["custName"] = "Sam Robinson"; // And address
    tCust.Rows[0]["custCity"] = "Chicago";
    tCust.Rows[1]["custCity"] = "Los Angeles";
    tCust.Rows[2]["custCity"] = "Washington";
    } private void AddCustomDataTableStyle()
    {
    DataGridTableStyle ts1 = new DataGridTableStyle();
    ts1.MappingName = "Customers";
    // Set other properties.
    ts1.AlternatingBackColor = Color.LightGray;
    //
    // Add textbox column style so we can catch textbox mouse clicks
    DataGridTextBoxColumn TextCol = new DataGridTextBoxColumn();
    TextCol.MappingName = "custID";
    TextCol.HeaderText = "CustomerID";
    TextCol.Width = 100;
    //add handler
    TextCol.TextBox.MouseDown += new MouseEventHandler(TextBoxMouseDownHandler);
    TextCol.TextBox.DoubleClick += new EventHandler(TextBoxDoubleClickHandler);
    ts1.GridColumnStyles.Add(TextCol); TextCol = new DataGridTextBoxColumn();
    TextCol.MappingName = "custName";
    TextCol.HeaderText = "Customer Name";
    TextCol.Width = 100;
    //add handler
    TextCol.TextBox.MouseDown += new MouseEventHandler(TextBoxMouseDownHandler);
    TextCol.TextBox.DoubleClick += new EventHandler(TextBoxDoubleClickHandler);
    ts1.GridColumnStyles.Add(TextCol); TextCol = new DataGridTextBoxColumn();
    TextCol.MappingName = "custCity";
    TextCol.HeaderText = "Customer Address";
    TextCol.Width = 100;
    //add handler
    TextCol.TextBox.MouseDown += new MouseEventHandler(TextBoxMouseDownHandler);
    TextCol.TextBox.DoubleClick += new EventHandler(TextBoxDoubleClickHandler);
    ts1.GridColumnStyles.Add(TextCol);

    dataGrid1.TableStyles.Add(ts1);

    } /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.dataGrid1 = new System.Windows.Forms.DataGrid();
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
    this.SuspendLayout();
    // 
    // dataGrid1
    // 
    this.dataGrid1.DataMember = "";
    this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
    this.dataGrid1.Location = new System.Drawing.Point(32, 24);
    this.dataGrid1.Name = "dataGrid1";
    this.dataGrid1.Size = new System.Drawing.Size(368, 144);
    this.dataGrid1.TabIndex = 0;
    this.dataGrid1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.dataGrid1_MouseDown);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(424, 189);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.dataGrid1});
    this.Name = "Form1";
    this.Text = "Form1";
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void TextBoxDoubleClickHandler(object sender, EventArgs e)
    {
    MessageBox.Show("TrueDoubleClick");
    } private void TextBoxMouseDownHandler(object sender, MouseEventArgs e)
    {
    if(DateTime.Now < gridMouseDownTime.AddMilliseconds(SystemInformation.DoubleClickTime))
    {
    MessageBox.Show("GridDoubleClick");
    }
    Console.WriteLine("TextBoxMouseDownHandler  " );
    } private void dataGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    gridMouseDownTime = DateTime.Now;
    Console.WriteLine("dataGrid1_MouseDown  " );
    }
    }
    }