datagridview CellContentClick事件

xlh776216378 2011-10-17 10:20:59
请问下,在datagridview中,选中莫行,这行莫列的值改变,而别的行同列的值不改变,该如何去做
...全文
461 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wnyxy001 2011-10-17
  • 打赏
  • 举报
回复
 
//假设你的第一列是button
if (dataGridView1.CurrentRow.Cells[0].Value.ToString() == "播放")
{
dataGridView1.CurrentRow.Cells[0].Value = "停止";
}
else
{
dataGridView1.CurrentRow.Cells[0].Value = "播放";
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (i != dataGridView1.CurrentRow.Index)
{
dataGridView1.Rows[i].Cells[0].Value = "停止";
}
}
}
xlh776216378 2011-10-17
  • 打赏
  • 举报
回复
表格是这么设计的,莫列PL6,对应的类型是button,PL6.text只有播放和停止两种,现在要判断,当选中莫行时候,当PL6是播放时候,别的列不能播放且PL6.text变成停止,请问该如何解决
wnyxy001 2011-10-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xlh776216378 的回复:]

描述清楚些,莫列全是button,点击其中一个按钮触发事件,别的按钮不变化
[/Quote]
你代码中只要不控制别的按钮,他们怎么会变化呢
xlh776216378 2011-10-17
  • 打赏
  • 举报
回复
描述清楚些,莫列全是button,点击其中一个按钮触发事件,别的按钮不变化
xlh776216378 2011-10-17
  • 打赏
  • 举报
回复
我的意思是在datagridview中莫列全是button,当点击时候别的列不变化
言多必失 2011-10-17
  • 打赏
  • 举报
回复
选中莫行,这行莫列的值改变. 什么意思? 是当前行的末列吗?

如果是 可以这样的 dataGridView1[dataGridView1.ColumnCount, dataGridView1.CurrentRow.Index].Value = "你自己的值";
wnyxy001 2011-10-17
  • 打赏
  • 举报
回复

private void datagridview1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
datagridview1.CurrentRow.Cells[0].Value = "qewqe";
}
wnyxy001 2011-10-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 xlh776216378 的回复:]

兄弟,能留个QQ吗?我发图片来交流下
[/Quote]
645045611
xlh776216378 2011-10-17
  • 打赏
  • 举报
回复
兄弟,能留个QQ吗?我发图片来交流下
wnyxy001 2011-10-17
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 xlh776216378 的回复:]

我试了一下,当不是播放时,dataGridView1.CurrentRow.Cell[0].Value并没有改变
[/Quote]
大哥 你都不去跟踪一下原因么?

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.CurrentRow.Cells[0].Value.ToString() == "播放")
{
dataGridView1.CurrentRow.Cells[0].Value = "停止";
}
else
{
//dataGridView1.CurrentRow.Cells[0].Value = "播放";
//for (int i = 0; i < dataGridView1.Rows.Count; i++)
//{
// if (i != dataGridView1.CurrentRow.Index)
// {
// dataGridView1.Rows[i].Cells[0].Value = "停止";
// }
//}
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].Cells[0].Value = "停止";
}
dataGridView1.CurrentRow.Cells[0].Value = "播放";
}

这样试试呢?两种方法我都测过了 不知道你那边什么原因
xlh776216378 2011-10-17
  • 打赏
  • 举报
回复
我试了一下,当不是播放时,dataGridView1.CurrentRow.Cell[0].Value并没有改变
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace 随机点名器 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } string a = ""; private void Form2_Load(object sender, EventArgs e) { // TODO: 这行代码将数据加载到表“dataSet1.学生信息”中。您可以根据需要移动或删除它。 // this.学生信息TableAdapter.Fill(this.dataSet1.学生信息); } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } private void button1_Click(object sender, EventArgs e) { String constr = "Server = 10.100.102.3;user id = dotnet;pwd = 123;database = wgz"; //String constr = @"data source = .;user id = ;pwd = ;database = TEST"; SqlConnection con = new SqlConnection(); con.ConnectionString = constr; con.Open(); // string dianming = "SELECT top 1 [学号] FROM [学生信息] ORDER BY NEWID()"; string sql = "SELECT top 1 * FROM [学生信息] ORDER BY NEWID()"; SqlDataAdapter da = new SqlDataAdapter(sql, con); DataSet ds = new DataSet(); da.Fill(ds, "cs"); dataGridView1.DataSource = ds.Tables["cs"]; a = ds.Tables[0].Rows[0][1].ToString(); con.Close(); } private void button2_Click(object sender, EventArgs e) { string b = a; String constr = "Server = 10.100.102.3;user id = dotnet;pwd = 123;database = wgz"; SqlConnection con = new SqlConnection(); con.ConnectionString = constr; con.Open(); string chuqin = "update [学生信息] set [出勤]=[出勤]+1 where [学号]='"+b+"'"; SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = chuqin; int i1 = cmd.ExecuteNonQuery(); string chuli = "update [学生信息] set [平时成绩] = [平时成绩] + 3 where [学号]='" + b + "'"; SqlCommand com = new SqlCommand(); com.Connection = con; com.CommandText = chuli; int i2 = com.ExecuteNonQuery(); string sql = "SELECT * FROM [学生信息] where [学号]='" + b + "'"; SqlDataAdapter da = new SqlDataAdapter(sql, con); DataSet ds = new DataSet(); da.Fill(ds, "cs"); dataGridView1.DataSource = ds.Tables["cs"]; con.Close(); } private void button3_Click(object sender, EventArgs e) { string b = a; String constr = "Server = 10.100.102.3;user id = dotnet;pwd = 123;database = wgz"; SqlConnection con = new SqlConnection(); con.ConnectionString = constr; con.Open(); string kuangke = "update [学生信息] set [旷课]=[旷课]+1 where [学号]='" + b + "'"; SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = kuangke; int i1 = cmd.ExecuteNonQuery(); string chuli = "update [学生信息] set [平时成绩] = [平时成绩] - 5 where [学号]='" + b + "'"; SqlCommand com = new SqlCommand(); com.Connection = con; com.CommandText = chuli; int i2 = com.ExecuteNonQuery(); string sql = "SELECT * FROM [学生信息] where [学号]='" + b + "'"; SqlDataAdapter da = new SqlDataAdapter(sql, con); DataSet ds = new DataSet(); da.Fill(ds, "cs"); dataGridView1.DataSource = ds.Tables["cs"]; con.Close(); } private void button4_Click(object sender, EventArgs e) { string b = a; String constr = "Server = 10.100.102.3;user id = dotnet;pwd = 123;database = wgz"; SqlConnection con = new SqlConnection(); con.ConnectionString = constr; con.Open(); string qingjia = "update [学生信息] set [请假]=[请假]+1 where [学号]='" + b + "'"; SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = qingjia; int i1 = cmd.ExecuteNonQuery(); string chuli = "update [学生信息] set [平时成绩] = [平时成绩] - 1 where [学号]='" + b + "'"; SqlCommand com = new SqlCommand(); com.Connection = con; com.CommandText = chuli; int i2 = com.ExecuteNonQuery(); string sql = "SELECT * FROM [学生信息] where [学号]='" + b + "'"; SqlDataAdapter da = new SqlDataAdapter(sql, con); DataSet ds = new DataSet(); da.Fill(ds, "cs"); dataGridView1.DataSource = ds.Tables["cs"]; con.Close(); } private void button5_Click(object sender, EventArgs e) { String constr = "Server = 10.100.102.3;user id = dotnet;pwd = 123;database = wgz"; SqlConnection con = new SqlConnection(); con.ConnectionString = constr; con.Open(); string sql = "SELECT * FROM [学生信息] ORDER BY [学号]"; SqlDataAdapter da = new SqlDataAdapter(sql, con); DataSet ds = new DataSet(); da.Fill(ds, "cs"); dataGridView1.DataSource = ds.Tables["cs"]; con.Close(); } private void button7_Click(object sender, EventArgs e) { Form1 form = new Form1(); form.Show(); this.Hide(); } } } 基于C# C/S的随机点名器
c# DBF数据库导入导出实例 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.Odbc; using System.Data.SqlClient; namespace DbfExample { public partial class Form1 : Form { System.Data.Odbc.OdbcConnection conn; public Form1() { InitializeComponent(); } //导出数据 private void btnOut_Click(object sender, EventArgs e) { string connect = "Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=c:\\; "; OdbcConnection myconn = new OdbcConnection(connect); string sqlt ="CREATE TABLE aa.DBF (cc int(10))"; myconn.Open(); OdbcCommand olec = new OdbcCommand(sqlt, myconn); try { int i = olec.ExecuteNonQuery(); MessageBox.Show("'" + i + "'success"); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { olec.Dispose(); myconn.Close(); } //string ole_connstring = @"Provider=microsoft.jet.oledb.5.0;Data Source=D:\;"; //System.Data.OleDb.OleDbConnection ole_conn = new System.Data.OleDb.OleDbConnection(ole_connstring); //try //{ // ole_conn.Open(); // System.Data.OleDb.OleDbCommand cmd1 = new System.Data.OleDb.OleDbCommand // ("Create Table TestTable (Field1 int, Field2 char(10),Field float(10,2))", // ole_conn); // System.Data.OleDb.OleDbCommand cmd2 = new System.Data.OleDb.OleDbCommand // ("Insert Into TestTable values (1,'Hello3',520.20)", ole_conn); // System.Data.OleDb.OleDbCommand cmd3 = new System.Data.OleDb.OleDbCommand // ("Insert Into TestTable values (2,'Hello4',18076.60)", ole_conn); // cmd1.ExecuteNonQuery(); // cmd2.ExecuteNonQuery(); // cmd3.ExecuteNonQuery(); //} //catch (Exception ex) //{ // MessageBox.Show(ex.Message); //} //finally //{ // ole_conn.Close(); //} } //导入数据 private void btnIn_Click(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { Bind(); } private void Bind() { try { conn = new System.Data.Odbc.OdbcConnection(); string table = @"C:\测试例子\Dbf\prepur.dbf"; string connStr = @"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + table + ";Exclusive=No;NULL=NO;Collate=Machine;BACKGROUNDFETCH=NO;DELETED=NO"; conn.ConnectionString = connStr; conn.Open(); string sql = @"select * from " + table; OdbcDataAdapter da = new OdbcDataAdapter(sql, conn); DataTable dt = new DataTable(); da.Fill(dt); this.dataGridView1.DataSource = dt.DefaultView; //MessageBox.Show(dt.Rows[0][0].ToString()); } catch { } finally { conn.Close(); } } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { try { conn = new System.Data.Odbc.OdbcConnection(); string table = @"C:\测试例子\Dbf\table1.dbf"; string connStr = @"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + table + ";Exclusive=No;NULL=NO;Collate=Machine;BACKGROUNDFETCH=NO;DELETED=NO"; conn.ConnectionString = connStr; conn.Open(); string id = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(); string sql = @"select * from " + table + " where id='" + id + "'"; OdbcDataAdapter da = new OdbcDataAdapter(sql, conn); DataTable dt = new DataTable(); da.Fill(dt); txtId.Text = id; txtName.Text = dt.Rows[0]["name"].ToString(); txtAddress.Text = dt.Rows[0]["address"].ToString(); } catch { } finally { conn.Close(); } } private void Add() { conn = new System.Data.Odbc.OdbcConnection(); string table = @"C:\temp\Dbf\table1.dbf"; string connStr = @"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + table + ";Exclusive=No;NULL=NO;Collate=Machine;BACKGROUNDFETCH=NO;DELETED=NO"; conn.ConnectionString = connStr; conn.Open(); OdbcCommand cmd = new OdbcCommand(); cmd.Connection = conn; string sql = "insert into " + table + " values('" + txtId.Text + "','" + txtName.Text + "','" + txtAddress.Text + "')"; cmd.CommandText = sql; cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); Bind(); } private void btnTOSQL_Click(object sender, EventArgs e) { try { string sql = "Insert Into dbftosql select * From openrowset('MSDASQL','Driver=Microsoft Visual FoxPro Driver;SourceType=DBF;SourceDB=C:\\temp\\Dbf','select * from table1.dbf')"; SqlConnection con = new SqlConnection("server=.;database=labelprint;uid=sa;pwd=sa"); con.Open(); SqlCommand cmd = new SqlCommand(sql, con); cmd.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using System.Timers; //Download by http://www.codefans.net namespace qwe { public partial class F_main : Form { //定义SqlDataAdapter的引用 SqlDataAdapter da = new SqlDataAdapter(); //定义Datast的引用 DataSet ds = new DataSet(); public F_main() { string skin = "skin/DiamondBlue.ssk"; InitializeComponent(); skinEngine1.SkinFile = skin; } private void F_main_Load(object sender, EventArgs e) { //定义SQL语句 string str_sql = string.Format("select cardid,cardnumber,cardbalance,transacttime from cardinfo"); //生成适配器实例 da = new SqlDataAdapter(str_sql,share.con); //填充数据 da.Fill(ds); //绑定数据源 dgv_data.DataSource = ds.Tables[0]; } public void shuaxin() { ds.Tables[0].Clear(); da.Fill(ds.Tables[0]); } private void btn_zc_Click(object sender, EventArgs e) { F_zhuce w = new F_zhuce(); w.ShowDialog(); } private void btn_close_Click(object sender, EventArgs e) { this.Close(); } private void btn_chaxun_Click(object sender, EventArgs e) { if (txt_ID.Text != "") { //执行SQL语句 string str_sql1 = string.Format("select count(*) from cardinfo where cardnumber = '{0}'", txt_ID.Text); //创建命名对象 SqlCommand cmd1 = new SqlCommand(str_sql1, share.con); //打开连接 share.con.Open(); //执行操作 int k = Convert.ToInt32(cmd1.ExecuteScalar()); share.con.Close(); if (k > 0) { F_chaxun a = new F_chaxun(); //执行SQL语句 string str_sql = string.Format("select cardnumber,cardpassword,cardbalance,transacttime from cardinfo where cardnumber = '{0}'", txt_ID.Text); //创建命名对象 SqlCommand cmd = new SqlCommand(str_sql, share.con); //打开连接 share.con.Open(); //执行操作 SqlDataReader reader = cmd.ExecuteReader(); //如果读取到数据就将其显示出来 if (reader.Read()) { a.txt_user.Text = reader[0].ToString(); a.txt_mima.Text = reader[1].ToString(); a.txt_yu_e.Text = reader[2].ToString(); a.txt_xiaji.Text = reader[3].ToString(); } share.con.Close(); a.ShowDialog(); } else { MessageBox.Show("没有该会员!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("请输入要查询的会员ID","操作提示",MessageBoxButtons.OK,MessageBoxIcon.Error); } } private void timer1_Tick(object sender, EventArgs e) { this.lbl_time.Text = "现在时间:"+DateTime.Now.ToString(); } private void F_main_FormClosed(object sender, FormClosedEventArgs e) { } private void F_main_FormClosed_1(object sender, FormClosedEventArgs e) { Application.Exit(); } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } private void 会员上机ToolStripMenuItem_Click(object sender, EventArgs e) { F_shangji b = new F_shangji(); b.ShowDialog(); } private void btn_updata_Click(object sender, EventArgs e) { F_updatapass m = new F_updatapass(); m.ShowDialog(); } private void btn_del_Click(object sender, EventArgs e) { F_del k = new F_del(); k.ShowDialog(); } private void btn_cz_Click(object sender, EventArgs e) { F_jiaofei qq = new F_jiaofei(); qq.ShowDialog(); } private void 会员下机ToolStripMenuItem_Click(object sender, EventArgs e) { F_xiaji t = new F_xiaji(); t.ShowDialog(); } private void 上机ToolStripMenuItem_Click(object sender, EventArgs e) { F_zhuce w = new F_zhuce(); w.ShowDialog(); } private void 下机ToolStripMenuItem_Click(object sender, EventArgs e) { F_updatapass m = new F_updatapass(); m.ShowDialog(); } private void 网卡充值ToolStripMenuItem_Click(object sender, EventArgs e) { F_jiaofei qq = new F_jiaofei(); qq.ShowDialog(); } private void 会员注销ToolStripMenuItem_Click(object sender, EventArgs e) { F_del k = new F_del(); k.ShowDialog(); } private void 退出系统ToolStripMenuItem_Click(object sender, EventArgs e) { this.Close(); } private void 关于ToolStripMenuItem_Click(object sender, EventArgs e) { F_help a = new F_help(); a.ShowDialog(); } private void 会员换机ToolStripMenuItem_Click(object sender, EventArgs e) { F_huanji1 a = new F_huanji1(); a.ShowDialog(); } private void panel2_Paint(object sender, PaintEventArgs e) { } private void 银色清爽ToolStripMenuItem_Click(object sender, EventArgs e) { string skin = "skin/MacOS.ssk"; skinEngine1.SkinFile = skin; } private void 黑色大气ToolStripMenuItem_Click(object sender, EventArgs e) { string skin = "skin/Calmness.ssk"; skinEngine1.SkinFile = skin; } private void 漂亮红色ToolStripMenuItem_Click(object sender, EventArgs e) { string skin = "skin/DiamondBlue.ssk"; skinEngine1.SkinFile = skin; } private void 绿色春意ToolStripMenuItem_Click(object sender, EventArgs e) { string skin = "skin/OneGreen.ssk"; skinEngine1.SkinFile = skin; } private void 黄色秋凉ToolStripMenuItem_Click(object sender, EventArgs e) { string skin = "skin/XPOrange.ssk"; skinEngine1.SkinFile = skin; } private void lbl_time_Click(object sender, EventArgs e) { } private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e) { shuaxin(); } private void cms_youjian_Opening(object sender, CancelEventArgs e) { } private void wuliaToolStripMenuItem_Click(object sender, EventArgs e) { F_zhuce w = new F_zhuce(); w.ShowDialog(); } private void 密码修改ToolStripMenuItem_Click(object sender, EventArgs e) { F_updatapass m = new F_updatapass(); m.ShowDialog(); } private void 网卡充值ToolStripMenuItem1_Click(object sender, EventArgs e) { F_jiaofei qq = new F_jiaofei(); qq.ShowDialog(); } private void 会会员注销ToolStripMenuItem_Click(object sender, EventArgs e) { F_del k = new F_del(); k.ShowDialog(); } private void 会员上机ToolStripMenuItem1_Click(object sender, EventArgs e) { F_shangji b = new F_shangji(); b.ShowDialog(); } private void 会员下机ToolStripMenuItem1_Click(object sender, EventArgs e) { F_xiaji t = new F_xiaji(); t.ShowDialog(); } private void 会员换机ToolStripMenuItem1_Click(object sender, EventArgs e) { F_huanji1 a = new F_huanji1(); a.ShowDialog(); } private void groupBox1_Enter(object sender, EventArgs e) { } private void panel4_Paint(object sender, PaintEventArgs e) { } private void pictureBox1_Click(object sender, EventArgs e) { } private void groupBox1_Enter_1(object sender, EventArgs e) { } private void label4_Click(object sender, EventArgs e) { } private void label5_Click(object sender, EventArgs e) { } private void groupBox4_Enter(object sender, EventArgs e) { } private void groupBox2_Enter(object sender, EventArgs e) { } private void dgv_data_SelectionChanged(object sender, EventArgs e) { if (dgv_data.SelectedRows.Count < 1) { return; } txt_name.Text = (string)dgv_data.SelectedRows[0].Cells["cardnumber"].Value; txt_balance.Text = Convert.ToString((int)dgv_data.SelectedRows[0].Cells["cardbalance"].Value); txt_time.Text = Convert.ToString(dgv_data.SelectedRows[0].Cells["transacttime"].Value); } private void button3_Click(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { } private void btn_jiqiadd_Click(object sender, EventArgs e) { } private void label5_Click_1(object sender, EventArgs e) { } private void groupBox3_Enter(object sender, EventArgs e) { } } }

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧