Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

c# - How to know a specific checkbox inside datagridview is checked or not?

i had a gridview which has 2 columns , one is textbox column and other is checkbox column, how to know which checkbox is checked .

enter image description here

As shown in image ,suppose any of the checkbox is checked , i want to display that the corresponding text box value to that checkbox.

can anyone help me?i tried the below code , but problem which i am facing is that , the values is getting displayed once i clicked to next checkbox then the previously checked checkbox values is getting displayed..

dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);

  void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {  
        object tempObj = dataGridView1.Rows[e.RowIndex].Cells[1].Value;
         dataGridView1_CurrentCellDirtyStateChanged(sender, e);

        if (((e.ColumnIndex) == 1) && ((bool)dataGridView1.Rows[e.RowIndex].Cells[1].Value))
        {
            MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());

        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
 private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        if (dataGridView1.IsCurrentCellDirty)
        {
            dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }
    }

    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        if (((e.ColumnIndex) == 1) && ((bool)dataGridView1.Rows[e.RowIndex].Cells[1].Value))
        {
            MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());

        }
    }

these below links helped me to understand the concept of cellvalue_changed and cell_content_click.. http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx

and by the help of these links i finally got the solution to my problem


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...