关于winform 中 datagrid 的问题 用什么方法可以得到鼠标在表上点击选择的记录,以方便操作该记录。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private void imageShow_DoubleClick(object sender, System.EventArgs e) { int y = imageShow.CurrentCell.ColumnNumber; int x = imageShow.CurrentCell.RowNumber; MessageBox.Show(imageShow[x,y].ToString());} imageShow_DoubleClick双击事件,你可以改其他的.. //基于WindowsForms取出DataGrid当前行绑定的DataTable的值。如果DataRowView drv = (DataRowView)this.BindingContext[this.ds,this.ds.Tables[0].TableName].Current;获取到drv就可以改数据,DataRow dw = drv.Row; // 得到DataRow,string s = dw["col1"].ToString();//想得到某一列的值DataTable dt = dw.Table;//得到DataTableDataSet ds = dt.DataSet; 如果需要更多的资料可以看这里,http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace 练习{/// <summary>/// Form1 的摘要说明。/// </summary>public class Form1 : System.Windows.Forms.Form{private System.Windows.Forms.DataGrid dataGrid1;private System.Windows.Forms.Button button1;private System.Windows.Forms.Button button2;private System.Windows.Forms.Button button3;private System.Windows.Forms.Button button4;/// <summary>/// 必需的设计器变量。/// </summary>private System.ComponentModel.Container components = null;public Form1(){//// Windows 窗体设计器支持所必需的//InitializeComponent();//// TODO: 在 InitializeComponent 调用后添加任何构造函数代码//}/// <summary>/// 清理所有正在使用的资源。/// </summary>protected override void Dispose( bool disposing ){if( disposing ){if (components != null) {components.Dispose();}}base.Dispose( disposing );}#region Windows 窗体设计器生成的代码/// <summary>/// 设计器支持所需的方法 - 不要使用代码编辑器修改/// 此方法的内容。/// </summary>private void InitializeComponent(){this.dataGrid1 = new System.Windows.Forms.DataGrid();this.button1 = new System.Windows.Forms.Button();this.button2 = new System.Windows.Forms.Button();this.button3 = new System.Windows.Forms.Button();this.button4 = new System.Windows.Forms.Button();((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();this.SuspendLayout();// // dataGrid1// this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;this.dataGrid1.DataMember = "";this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Top;this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;this.dataGrid1.Location = new System.Drawing.Point(0, 0);this.dataGrid1.Name = "dataGrid1";this.dataGrid1.Size = new System.Drawing.Size(496, 168);this.dataGrid1.TabIndex = 0;// // button1// this.button1.Location = new System.Drawing.Point(16, 184);this.button1.Name = "button1";this.button1.Size = new System.Drawing.Size(104, 24);this.button1.TabIndex = 1;this.button1.Text = "添加";this.button1.Click += new System.EventHandler(this.button1_Click);// // button2// this.button2.Location = new System.Drawing.Point(136, 184);this.button2.Name = "button2";this.button2.Size = new System.Drawing.Size(104, 24);this.button2.TabIndex = 2;this.button2.Text = "删除";this.button2.Click += new System.EventHandler(this.button2_Click);// // button3// this.button3.Location = new System.Drawing.Point(256, 184);this.button3.Name = "button3";this.button3.Size = new System.Drawing.Size(104, 24);this.button3.TabIndex = 3;this.button3.Text = "修改";this.button3.Click += new System.EventHandler(this.button3_Click);// // button4// this.button4.Location = new System.Drawing.Point(376, 184);this.button4.Name = "button4";this.button4.Size = new System.Drawing.Size(104, 24);this.button4.TabIndex = 4;this.button4.Text = "退出";this.button4.Click += new System.EventHandler(this.button4_Click);// // Form1// this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);this.ClientSize = new System.Drawing.Size(496, 230);this.Controls.Add(this.button4);this.Controls.Add(this.button3);this.Controls.Add(this.button2);this.Controls.Add(this.button1);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>/// 应用程序的主入口点。/// </summary>[STAThread]static void Main() {Application.Run(new Form1());}System.Data.SqlClient.SqlDataAdapter da;System.Data.SqlClient.SqlParameter par;System.Data.DataTable dt;data d=new data();//这是一个外部类,我只是调用了一下,里面有实现连接数据库和一个简单的查询方法string sql;public void load(){try{dt=d.select("select * from admin");this.dataGrid1.DataSource=dt;}catch(Exception ex){MessageBox.Show(ex.Message);}}private void Form1_Load(object sender, System.EventArgs e){load();}private void button1_Click(object sender, System.EventArgs e)//添加{try{sql="insert into admin values(@name,@pwd)";da=new System.Data.SqlClient.SqlDataAdapter();da.InsertCommand=new System.Data.SqlClient.SqlCommand(sql,data.conn);par=da.InsertCommand.Parameters.Add("@name",System.Data.SqlDbType.Int);par.SourceColumn="name";par.SourceVersion=System.Data.DataRowVersion.Current;par=da.InsertCommand.Parameters.Add("@pwd",System.Data.SqlDbType.Int);par.SourceColumn="pwd";par.SourceVersion=System.Data.DataRowVersion.Current;da.Update(dt);dt.Clear();load();}catch(Exception ex){MessageBox.Show(ex.Message);}}private void button2_Click(object sender, System.EventArgs e)//删除{try{sql="delete from admin where id=@id";da=new System.Data.SqlClient.SqlDataAdapter();da.DeleteCommand=new System.Data.SqlClient.SqlCommand(sql,data.conn);par=da.DeleteCommand.Parameters.Add("@id",System.Data.SqlDbType.Int);par.SourceColumn="id";par.SourceVersion=System.Data.DataRowVersion.Original;dt.Rows[this.dataGrid1.CurrentRowIndex].Delete();da.Update(dt);dt.Clear();load();}catch(Exception ex){MessageBox.Show(ex.Message);}}private void button3_Click(object sender, System.EventArgs e)//修改{try{sql="update admin set name=@name,pwd=@pwd where id=@id";da=new System.Data.SqlClient.SqlDataAdapter();da.UpdateCommand=new System.Data.SqlClient.SqlCommand(sql,data.conn);par=da.UpdateCommand.Parameters.Add("@name",System.Data.SqlDbType.Int);par.SourceColumn="name";par.SourceVersion=System.Data.DataRowVersion.Current;par=da.UpdateCommand.Parameters.Add("@pwd",System.Data.SqlDbType.Int);par.SourceColumn="pwd";par.SourceVersion=System.Data.DataRowVersion.Current;par=da.UpdateCommand.Parameters.Add("@id",System.Data.SqlDbType.Int);par.SourceColumn="id";par.SourceVersion=System.Data.DataRowVersion.Original;da.Update(dt);dt.Clear();load();}catch(Exception ex){MessageBox.Show(ex.Message);}}private void button4_Click(object sender, System.EventArgs e){this.Close();}}} 字符串替换求助 如何替换word中书签的内容 datagrid列数据保存到本地(.TXT)格式的 select * from abc,会不会把abc都全部选择出来?? @@@@ C#字符串难题 ,寻求解决方法 @@@@ 字符串大小写操作的问题 Imp简写,一般代表什么意思? asp.net dopostback is not defined 各位前辈!!!哪位有<INSIDE C# 中文版 >的下载地址,望告之,谢谢 求救,一个很奇怪的问题!!(关于Windows 服务) xmlns 添加XML节点时不想加这个节点 怎么让一个函数响应多个按钮啊?
{
int y = imageShow.CurrentCell.ColumnNumber;
int x = imageShow.CurrentCell.RowNumber;
MessageBox.Show(imageShow[x,y].ToString());
}
双击事件,你可以改其他的..
取出DataGrid当前行绑定的DataTable的值。如果DataRowView drv = (DataRowView)this.BindingContext[this.ds,this.ds.Tables[0].TableName].Current;
获取到drv就可以改数据,
DataRow dw = drv.Row; // 得到DataRow,
string s = dw["col1"].ToString();//想得到某一列的值
DataTable dt = dw.Table;//得到DataTable
DataSet ds = dt.DataSet;
如果需要更多的资料可以看这里,
http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace 练习
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.dataGrid1.DataMember = "";
this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Top;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 0);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(496, 168);
this.dataGrid1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 184);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 24);
this.button1.TabIndex = 1;
this.button1.Text = "添加";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(136, 184);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(104, 24);
this.button2.TabIndex = 2;
this.button2.Text = "删除";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(256, 184);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(104, 24);
this.button3.TabIndex = 3;
this.button3.Text = "修改";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(376, 184);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(104, 24);
this.button4.TabIndex = 4;
this.button4.Text = "退出";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(496, 230);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
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>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
System.Data.SqlClient.SqlDataAdapter da;
System.Data.SqlClient.SqlParameter par;
System.Data.DataTable dt;
data d=new data();//这是一个外部类,我只是调用了一下,里面有实现连接数据库和一个简单的查询方法
string sql;
public void load()
{
try
{
dt=d.select("select * from admin");
this.dataGrid1.DataSource=dt;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
load();
}private void button1_Click(object sender, System.EventArgs e)//添加
{
try
{
sql="insert into admin values(@name,@pwd)";
da=new System.Data.SqlClient.SqlDataAdapter();
da.InsertCommand=new System.Data.SqlClient.SqlCommand(sql,data.conn);par=da.InsertCommand.Parameters.Add("@name",System.Data.SqlDbType.Int);
par.SourceColumn="name";
par.SourceVersion=System.Data.DataRowVersion.Current;par=da.InsertCommand.Parameters.Add("@pwd",System.Data.SqlDbType.Int);
par.SourceColumn="pwd";
par.SourceVersion=System.Data.DataRowVersion.Current;da.Update(dt);
dt.Clear();
load();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}private void button2_Click(object sender, System.EventArgs e)//删除
{
try
{
sql="delete from admin where id=@id";
da=new System.Data.SqlClient.SqlDataAdapter();
da.DeleteCommand=new System.Data.SqlClient.SqlCommand(sql,data.conn);par=da.DeleteCommand.Parameters.Add("@id",System.Data.SqlDbType.Int);
par.SourceColumn="id";
par.SourceVersion=System.Data.DataRowVersion.Original;dt.Rows[this.dataGrid1.CurrentRowIndex].Delete();da.Update(dt);
dt.Clear();
load();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}private void button3_Click(object sender, System.EventArgs e)//修改
{
try
{
sql="update admin set name=@name,pwd=@pwd where id=@id";
da=new System.Data.SqlClient.SqlDataAdapter();
da.UpdateCommand=new System.Data.SqlClient.SqlCommand(sql,data.conn);par=da.UpdateCommand.Parameters.Add("@name",System.Data.SqlDbType.Int);
par.SourceColumn="name";
par.SourceVersion=System.Data.DataRowVersion.Current;par=da.UpdateCommand.Parameters.Add("@pwd",System.Data.SqlDbType.Int);
par.SourceColumn="pwd";
par.SourceVersion=System.Data.DataRowVersion.Current;par=da.UpdateCommand.Parameters.Add("@id",System.Data.SqlDbType.Int);
par.SourceColumn="id";
par.SourceVersion=System.Data.DataRowVersion.Original;da.Update(dt);
dt.Clear();
load();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}private void button4_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}