Eventhandling in Datagrid

Hallo,

I want to know how to implement the Eventhandling  such as doubleclick  in Datagrids and how to display the record in the selected row. Can anybody please help me

thanks n regards
swingme




Answer this question

Eventhandling in Datagrid

  • hocki101

    Maybe this will help



    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Data.SqlClient;

    namespace DGDoubleClick
    {
     /// <summary>
     /// Summary description for Form1.
     /// </summary>
     public class Form1 : System.Windows.Forms.Form
     {
      private System.Windows.Forms.DataGrid dataGrid1;
      /// <summary>
      /// Required designer variable.
      /// </summary>
      private System.ComponentModel.Container components = null;

      public Form1()
      {
       //
       // Required for Windows Form Designer support
       //
       InitializeComponent();

       //
       // TODO: Add any constructor code after InitializeComponent call
       //
      }

      /// <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(16, 40);
       this.dataGrid1.Name = "dataGrid1";
       this.dataGrid1.Size = new System.Drawing.Size(392, 192);
       this.dataGrid1.TabIndex = 0;
       //
       // Form1
       //
       this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
       this.ClientSize = new System.Drawing.Size(432, 266);
       this.Controls.Add(this.dataGrid1);
       this.Name = "Form1";
       this.Text = "Form1";
       this.Load += new System.EventHandler(this.Form1_Load);
       ((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 Form1_Load(object sender, System.EventArgs e)
      {
       DataSet ds = new DataSet();
       string strConn = "Server = (local);Database = NorthWind; Integrated Security = SSPI;";
       SqlConnection conn = new SqlConnection(strConn);
       SqlDataAdapter da = new SqlDataAdapter("Select * From Employees", conn);
       da.Fill(ds, "Employees");
       dataGrid1.DataSource = ds;
       dataGrid1.DataMember = "Employees";
       DataGridTableStyle ts = new DataGridTableStyle();
       ts.MappingName = "Employees";
       DataGridTextBoxColumn colLast = new DataGridTextBoxColumn();
       colLast.MappingName="LastName";
       colLast.HeaderText= "Last Name";
       colLast.Width=150;
       colLast.TextBox.DoubleClick +=new EventHandler(Column_DoubleClick);
       DataGridTextBoxColumn colFirst = new DataGridTextBoxColumn();
       colFirst.MappingName="FirstName";
       colFirst.HeaderText = "First Name";
       colFirst.Width=150;
       colFirst.TextBox.DoubleClick +=new EventHandler(Column_DoubleClick);
       ts.GridColumnStyles.Add(colFirst);
       ts.GridColumnStyles.Add(colLast);
       dataGrid1.TableStyles.Add(ts);
       colLast.Dispose();
       colFirst.Dispose();
      }

      private void Column_DoubleClick(object sender, System.EventArgs e)
      {
       int x = dataGrid1.CurrentCell.RowNumber;
       int y = dataGrid1.CurrentCell.ColumnNumber;
       MessageBox.Show(dataGrid1[x, y].ToString());
      }
     }
    }

     



  • Jeff A Beck

    Hallo Ken,

    It di'nt really helped me Tongue Tied. When i double click in the table,  does'nt capture the event. How will i get the values of the row, in which i double clicked

    regards
    swingme

  • elesser

    Double click
    http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q869q

    I dont understand the second part of the question but you might find the answer in one of these links

    http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx

    http://www.vb-tips.com/

  • John Penberthy

    Hallo Ken,
    thanks for  ur help.the example worked.but however not in my program....have to dig it deep...

    regards
    swingme

  • philmee95

    Hallo ken,

    thanks for ur response. i will refer the sites. i mean, how to retrieve the values in the selected row.

    regards
    swingme



  • Eventhandling in Datagrid