使用复选框选择连接三个datagridviews [英] Connecting three datagridviews using checkbox selection

查看:51
本文介绍了使用复选框选择连接三个datagridviews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单中创建了三个datagridviews。所有三个gridviews数据仅来自一个表。一个gridview包含(itmcod,title,procod,typcod,title2)这些字段。第二个gridview包含(typetitle,procod,typcod,title2)。第三个gridview包含title2,typcod。所有三个gridviews都有一些独特的字段(procod,typcod)。如果我自动从datagridview2中选择一个值(使用勾选复选框),则应选中具有相同procod和typcod值行的其他两个gridview,并选中复选框selecion。



我尝试了什么:



我已将三个表链接到一个临时表中,并从temptable中将数据转换为gridview。但我不知道如何使用复选框选择这个

I have created three datagridviews in form. All three gridviews datas are from only one table. One gridview contains (itmcod,title,procod,typcod,title2) these fields. Second gridview contains (typetitle,procod,typcod,title2). And the third gridview contains title2,typcod. All three gridviews have some unique fields (procod, typcod). If i select one value from datagridview2 (using checking the checkbox) automatically the other two gridviews which has the same procod and typcod value row should be selected with check box selecion.

What I have tried:

I've linked three tables into one temporary table and retreived datas to gridview from temptable. but i dont no how to use checkbox selection option for this

推荐答案

结果比我想象的更加繁琐!



选择适当的事件来放置代码是第一个问题。 CellValidated或CellValueChanged是我的第一个想法,但是当我预期时这些没有被触发(例如,单击复选框时没有触发CellValidated,但是当你点击其他地方时 - 不是很好的用户体验)



我最终选择 dataGridView2_CellClick 事件,这会带来它自己的问题...当检查 .Value 第一次点击时你会发现它是 null 。所以我不得不使用 EditingCellFormattedValue - 麻烦的是,在事件被触发时,这个值仍然是我们认为它的相反 ...换句话说,当我们点击勾选框时,这个值仍然是假的 - 它具有我们从移动的值而不是我们正在移动的值。我正在努力这一点,以引起对
This turned out to be more fiddly than I expected!

Choosing the appropriate event to put code behind was the first problem. CellValidated or CellValueChanged were my first thoughts, but these were not being fired when I expected (for example CellValidated is not fired when the checkbox is clicked, but when you click elsewhere - not a good user experience)

I ended up choosing the dataGridView2_CellClick event, which brings it's own problems ... when checking the .Value of the cell on the first click you will find it is null. So I had to use EditingCellFormattedValue instead - trouble is, at the time the event is fired this value is still the opposite of what we think it is ... in other words when we are clicking the tick box this value is still false - it has the value we are moving from not the value we are moving to. I'm labouring this point a little to draw attention to the not (!) operator in the line
var checkValue = !(bool)((DataGridViewCheckBoxCell)dataGridView2.Rows[r].Cells[c]).EditingCellFormattedValue;



要考虑的下一个问题是属性 AllowUserToAddRows 是否已设置为true或false(默认值为true)。在下面的代码中,我处理任何一个,但如果你在使用 foreach 时省略了将其设置为false,你将得到一个异常


The next issue to consider is whether or not the properties AllowUserToAddRows has been set to true or false (the default is true). In the code below I handle either, but if you omit the setting of this to false when using the foreach you will get an exception

Quote:

Sandbox.exe中出现未处理的System.NullReferenceException类型异常



附加信息:未设置对象引用到一个对象的实例。

An unhandled exception of type 'System.NullReferenceException' occurred in Sandbox.exe

Additional information: Object reference not set to an instance of an object.





这是我的完整解决方案



Here is my full solution

private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
    var r = e.RowIndex;
    var c = e.ColumnIndex;

    if (dataGridView2.Rows[r].Cells[c].GetType() != typeof(DataGridViewCheckBoxCell)) return;

    var procodSearch = dataGridView2.Rows[r].Cells[2].Value.ToString();
    var typcodSearch = dataGridView2.Rows[r].Cells[3].Value.ToString();
    var checkValue = !(bool)((DataGridViewCheckBoxCell)dataGridView2.Rows[r].Cells[c]).EditingCellFormattedValue;

    var temp = dataGridView1.AllowUserToAddRows;
    dataGridView1.AllowUserToAddRows = false;

    foreach (DataGridViewRow dr in dataGridView1.Rows)
    {
        if (dr.Cells[dataGridView1.Columns["procod"].Index].Value.ToString().Equals(procodSearch)
            && dr.Cells[dataGridView1.Columns["typcod"].Index].Value.ToString().Equals(typcodSearch))
            ((DataGridViewCheckBoxCell)dr.Cells[0]).Value = checkValue;
    }
    dataGridView1.AllowUserToAddRows = temp;

    temp = dataGridView3.AllowUserToAddRows;
    dataGridView3.AllowUserToAddRows = false;

    foreach (DataGridViewRow dr in dataGridView3.Rows)
    {
        if (dr.Cells[dataGridView3.Columns["typcod"].Index].Value.ToString().Equals(typcodSearch))
            ((DataGridViewCheckBoxCell)dr.Cells[0]).Value = checkValue;
    }
    dataGridView3.AllowUserToAddRows = temp;

}



或者如果您更喜欢使用Linq:


Or if you prefer to use Linq:

private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
    var r = e.RowIndex;
    var c = e.ColumnIndex;

    if (dataGridView2.Rows[r].Cells[c].GetType() != typeof(DataGridViewCheckBoxCell)) return;

    var procodSearch = dataGridView2.Rows[r].Cells["procod"].Value.ToString();
    var typcodSearch = dataGridView2.Rows[r].Cells["typcod"].Value.ToString();
    var checkValue = !(bool)((DataGridViewCheckBoxCell)dataGridView2.Rows[r].Cells[c]).EditingCellFormattedValue;

    var temp = dataGridView1.AllowUserToAddRows;
    dataGridView1.AllowUserToAddRows = false;

    foreach (DataGridViewRow dr in dataGridView1.Rows.Cast<DataGridViewRow>()
        .Where(dr => dr.Cells[dataGridView1.Columns["procod"].Index].Value.ToString().Equals(procodSearch)
           && dr.Cells[dataGridView1.Columns["typcod"].Index].Value.ToString().Equals(typcodSearch)))
        ((DataGridViewCheckBoxCell)dr.Cells[0]).Value = checkValue;

    dataGridView1.AllowUserToAddRows = temp;

    temp = dataGridView3.AllowUserToAddRows;
    dataGridView3.AllowUserToAddRows = false;

    foreach (DataGridViewRow dr in dataGridView3.Rows.Cast<DataGridViewRow>()
        .Where(dr => dr.Cells[dataGridView3.Columns["typcod"].Index].Value.ToString().Equals(typcodSearch)))
        ((DataGridViewCheckBoxCell)dr.Cells[0]).Value = checkValue;

    dataGridView3.AllowUserToAddRows = temp;

}



这不是一个理想的解决方案 - 例如我已经假设哪些列包含CheckBoxes(我假设列上面的0)并且没有检查任何列中的空值,但它是一个开始。


This is not an ideal solution - for example I have made some assumptions about which columns contain the CheckBoxes (I've assumed column 0 above) and there is no checking for null values in any columns, but it's a start.


这篇关于使用复选框选择连接三个datagridviews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆