这个是可以实现的,我给你一个我的代码: private void SetUp() { string select="SELECT SUP_NO, COMPANY_NAME, ADDRESS FROM t_b_SUPPLIER WHERE STATUS!=9 ORDER BY SUP_NO ASC"; SqlDataAdapter da = new SqlDataAdapter(select,conn); DataSet ds=new DataSet(); da.Fill(ds,"t_b_SUPPLIER"); dataGrid1.SetDataBinding(ds,"t_b_SUPPLIER"); }
全部代码如下: 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"); this.Close(); } 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 " ); } } }
参考这个网址。
要是这个也不知道的话那没办法了
{
if(ds!=null)
{
if((MessageBox.Show("确定要删除此记录吗?","删除记录操作",MessageBoxButtons.YesNo))==DialogResult.Yes)
{
using(SqlConnection conn=new SqlConnection("server=.;uid=sa;pwd=ok;database=musicmanagment"))
{
string sql=" delete musicTable where Musicid="+ds.Tables[0].
Rows[this.BindingContext[ds.Tables[0]].Position][0];
ds.Tables[0].Rows[this.BindingContext[ds.Tables[0]].Position].Delete();
SqlCommand com=new SqlCommand(sql,conn);
conn.Open();
int i=com.ExecuteNonQuery();
conn.Close();
this.dataGrid1.DataSource=ds.Tables[0];
if(i>0)
{
MessageBox.Show("删除成功!");
}
}
}
} }这样可以写出在datagrid中的单击事件,确定删除的行数不知是不是这个呢~~上面只是一个例子.
private void SetUp()
{
string select="SELECT SUP_NO, COMPANY_NAME, ADDRESS FROM t_b_SUPPLIER WHERE STATUS!=9 ORDER BY SUP_NO ASC";
SqlDataAdapter da = new SqlDataAdapter(select,conn);
DataSet ds=new DataSet();
da.Fill(ds,"t_b_SUPPLIER");
dataGrid1.SetDataBinding(ds,"t_b_SUPPLIER");
}
private void AddCustomDataTableStyle()
{
DataGridTableStyle ts1 = new DataGridTableStyle();
ts1.MappingName = "t_b_SUPPLIER";
// 设置属性
ts1.AlternatingBackColor = Color.LightGray;
// 添加Textbox列样式,以便我们捕捉鼠标事件
DataGridTextBoxColumn TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "SUP_NO";
TextCol.HeaderText = "供应商编号";
TextCol.Width = 100;
TextCol.ReadOnly = true;
//添加事件处理器
TextCol.TextBox.Click += new EventHandler(TextBoxDoubleClickHandler);
ts1.GridColumnStyles.Add(TextCol);
TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "COMPANY_NAME";
TextCol.HeaderText = "供应商名称";
TextCol.Width = 150;
TextCol.ReadOnly = true;
TextCol.TextBox.Click += new EventHandler(TextBoxDoubleClickHandler);
ts1.GridColumnStyles.Add(TextCol);
TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "ADDRESS";
TextCol.HeaderText = "供应商地址";
TextCol.Width = 200;
TextCol.ReadOnly = true;
TextCol.TextBox.Click += new EventHandler(TextBoxDoubleClickHandler);
ts1.GridColumnStyles.Add(TextCol); ts1.SelectionBackColor = System.Drawing.Color.Blue; dataGrid1.TableStyles.Add(ts1); }
private void dataGrid1_CurrentCellChanged(object sender,System.EventArgs e)
{
dataGrid1.Select(this.dataGrid1.CurrentCell.RowNumber);
} private void TextBoxDoubleClickHandler(object sender, EventArgs e)
{
SUP_NO = this.dataGrid1[this.dataGrid1.CurrentCell.RowNumber,0].ToString();
contract_sp_new myForm = new contract_sp_new();
myForm.ShowDialog();
}
在FormLoad中要加上 AddCustomDataTableStyle();
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");
this.Close();
} 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 " );
}
}
}
{
DataGrid myGrid = (DataGrid) sender;
System.Windows.Forms.DataGrid.HitTestInfo hti;
hti = myGrid.HitTest(e.X, e.Y);
string message = "You clicked ";
switch (hti.Type)
{
case System.Windows.Forms.DataGrid.HitTestType.Cell :
message += "cell at row " + hti.Row + ", col " + hti.Column;
break;
case System.Windows.Forms.DataGrid.HitTestType.ColumnHeader :
message += "the column header for column " + hti.Column;
break;
case System.Windows.Forms.DataGrid.HitTestType.RowHeader :
message += "the row header for row " + hti.Row;
break;
case System.Windows.Forms.DataGrid.HitTestType.Caption :
message += "the caption";
break;
case System.Windows.Forms.DataGrid.HitTestType.ParentRows :
message += "the parent row";
break;
} MessageBox.Show(message);
}
试试,单击完全可以。
另外,这个方法只适用于单击。如果双击,我看可以参考楼上的!
的bug