首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

关于winform 中 datagrid 的有关问题

2012-01-07 
关于winform 中 datagrid 的问题用什么方法可以得到鼠标在表上点击选择的记录,以方便操作该记录。[解决办法

关于winform 中 datagrid 的问题
用什么方法可以得到鼠标在表上点击选择的记录,以方便操作该记录。

[解决办法]
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());
}
[解决办法]
//基于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;//得到DataTable
DataSet 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();
}
}
}

热点排行