C#WinForm中DataGridView控件中代码来控制选择单元格或行或列,怎么写呢?内详 如题....详细:我在一窗体上添加了两个dataGridView控件,我想在我点击dataGridView1时,dataGridView2的也同时选中与dataGridView1相同的单元格,或是行,或是列...麻烦你们了. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在DataGridView1的SelectedIndexChanged事件中,遍历DataGridView2查找相同的Cell,然后设置DataGridView2的SelectedIndex。 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { dataGridView2[e.ColumnIndex, e.RowIndex].Selected = true; //单元格 //dataGridView2.Rows[e.RowIndex].Selected = true; //行 //dataGridView2.Columns[e.ColumnIndex].Selected = true; //列 } dataGridView控件是没有SelectedIndexChange事件.汗...我给我的代码给你们看看:按逻辑我的代码没错.但是运行的时候,就出现一些不理想的效果.如:我随便选中了dataGridView1中的某一个单元格,而这时dataGridView2中的只能选中和dataGridView1的列索引相同的单元格.不过我第一次选中dataGridView1的时候,dataGridView2却可以正常的选择与dataGridView1相同的单元格.代码如下: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.SqlClient;namespace test.TestDataGridView{ public partial class TestDataGridView : Form { public TestDataGridView() { InitializeComponent(); loadDataSource(); this.dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect; this.dataGridView1.Columns.Clear(); this.dataGridView2.Columns.Clear(); this.dataGridView1.DataSource = ds.Tables[0]; this.dataGridView2.DataSource = ds.Tables[0]; } private string con_str = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ShoesManager;Data Source=JAVELIN\SQLEXPRESS"; private string select_str = "select * from Goods"; private DataSet ds; private SqlConnection con; private SqlDataAdapter dataDapter; int colindex = 0; int rowindex = 0; private void loadDataSource() { con = new SqlConnection(con_str); dataDapter = new SqlDataAdapter(select_str, con); ds = new DataSet(); dataDapter.Fill(ds); } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { this.dataGridView2[colindex, rowindex].Selected = false; this.dataGridView2[e.ColumnIndex, e.RowIndex].Selected = true; colindex = e.ColumnIndex; rowindex = e.RowIndex; } }} 该朋友的可以实现.不过还有一个问题.就是dataGridView2中的,始选择项,一直存在,不会消失. 加一句:dataGridView2.CurrentCell = dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex]; 大神 小弟求救 winForm设计,可不可以通过外部类动态修改控件的属性?? 为什么Graphics对象不能用? C# 应用程序如何上传附件到数据库,并读取附件. 更新了数据库怎么做sql中看不到添加到信息 请教怎么在网页中点击删除键后,弹出确认删除对话框?? 求教几个关于水晶报表的入门问题(欢迎灌水) 如何给datagrid中的textbox赋数据库中得值? c#中的串口通讯。 我用c#做的asp程序为什么只能显示label控件不能显示如文本框等其它控件 C#的一个程序问题,小问题求教。 enterprise library4 logging 的时间与系统时间不一致....大家来看看
{
dataGridView2[e.ColumnIndex, e.RowIndex].Selected = true; //单元格
//dataGridView2.Rows[e.RowIndex].Selected = true; //行
//dataGridView2.Columns[e.ColumnIndex].Selected = true; //列
}
dataGridView控件是没有SelectedIndexChange事件.汗...我给我的代码给你们看看:按逻辑我的代码没错.但是运行的时候,就出现一些不理想的效果.如:我随便选中了dataGridView1中的某一个单元格,而这时dataGridView2中的只能选中和dataGridView1的列索引相同的单元格.不过我第一次选中dataGridView1的时候,dataGridView2却可以正常的选择与dataGridView1相同的单元格.代码如下:
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.SqlClient;namespace test.TestDataGridView
{
public partial class TestDataGridView : Form
{
public TestDataGridView()
{
InitializeComponent();
loadDataSource();
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
this.dataGridView1.Columns.Clear();
this.dataGridView2.Columns.Clear();
this.dataGridView1.DataSource = ds.Tables[0];
this.dataGridView2.DataSource = ds.Tables[0];
} private string con_str = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ShoesManager;Data Source=JAVELIN\SQLEXPRESS";
private string select_str = "select * from Goods";
private DataSet ds;
private SqlConnection con;
private SqlDataAdapter dataDapter;
int colindex = 0;
int rowindex = 0; private void loadDataSource()
{
con = new SqlConnection(con_str);
dataDapter = new SqlDataAdapter(select_str, con);
ds = new DataSet();
dataDapter.Fill(ds);
} private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
this.dataGridView2[colindex, rowindex].Selected = false;
this.dataGridView2[e.ColumnIndex, e.RowIndex].Selected = true;
colindex = e.ColumnIndex;
rowindex = e.RowIndex;
}
}
}
该朋友的可以实现.不过还有一个问题.就是dataGridView2中的,始选择项,一直存在,不会消失.
dataGridView2.CurrentCell = dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex];