2个回答
展开全部
因为 datagridview 没有提供这样的属性和方法,我们可以变通一下,用两个datagridview完全可以实现你所要求的功能,完全看不出是两个拼装的,用于显示别的数据时将datagridview2隐藏即可。如果你有兴趣改写一下,把它封装成类,可以实现通用。方法如下:(仅供参考)
其中:mylj.Alink("select * from rjzh") 是我封装的获取数据的类的方法。
private void Form1_Load(object sender, EventArgs e)
{
//需要时调用myview()方法。
myview();
//不需要调用myview()方法时隐藏 dataGridView2
dataGridView2.Visible =false;
//以上两句只能选一句。
}
private void button1_Click(object sender, EventArgs e)
{
myview();//调用myview()方法
}
private void myview()
{
dataGridView1.DataSource = null;
mylj.Alink("select * from rjzh");
dataGridView1.DataSource = mylj.vew;
mylj.Alink("select 编码 from rjzh");
dataGridView2.Visible = true;
dataGridView2.DataSource = mylj.vew;
dataGridView2.BorderStyle = 0;
dataGridView2.ScrollBars = 0;
dataGridView2.Rows[0].Selected = false;
dataGridView2.Width = dataGridView1.Columns[dataGridView1.Columns.Count - 1].Width + 2;
dataGridView2.Left = dataGridView1.Left + dataGridView1.Width - dataGridView2.Width - 22;
dataGridView2.Top = dataGridView1.Top + 1;
dataGridView2.Height = dataGridView1.Height - 23;
dataGridView2.RowHeadersVisible = false;
dataGridView2.Columns[0].Width = dataGridView1.Columns[dataGridView1.Columns.Count - 1].Width;
}
private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
dataGridView2.FirstDisplayedScrollingRowIndex = dataGridView1.FirstDisplayedScrollingRowIndex;
}
其中:mylj.Alink("select * from rjzh") 是我封装的获取数据的类的方法。
private void Form1_Load(object sender, EventArgs e)
{
//需要时调用myview()方法。
myview();
//不需要调用myview()方法时隐藏 dataGridView2
dataGridView2.Visible =false;
//以上两句只能选一句。
}
private void button1_Click(object sender, EventArgs e)
{
myview();//调用myview()方法
}
private void myview()
{
dataGridView1.DataSource = null;
mylj.Alink("select * from rjzh");
dataGridView1.DataSource = mylj.vew;
mylj.Alink("select 编码 from rjzh");
dataGridView2.Visible = true;
dataGridView2.DataSource = mylj.vew;
dataGridView2.BorderStyle = 0;
dataGridView2.ScrollBars = 0;
dataGridView2.Rows[0].Selected = false;
dataGridView2.Width = dataGridView1.Columns[dataGridView1.Columns.Count - 1].Width + 2;
dataGridView2.Left = dataGridView1.Left + dataGridView1.Width - dataGridView2.Width - 22;
dataGridView2.Top = dataGridView1.Top + 1;
dataGridView2.Height = dataGridView1.Height - 23;
dataGridView2.RowHeadersVisible = false;
dataGridView2.Columns[0].Width = dataGridView1.Columns[dataGridView1.Columns.Count - 1].Width;
}
private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
dataGridView2.FirstDisplayedScrollingRowIndex = dataGridView1.FirstDisplayedScrollingRowIndex;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询