Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two DataGridView's: DataGridView2, DataGridView3.

I have checkboxes in DataGridView2 in a column named "Column1".

I want to be able to select multiple rows in DataGridView2 and click and button and DataGridView3 would contain only the checkboxed rows.

This is what I have so far in the "Transfer" button:

dataGridView3.Rows.Clear();
            string x = string.Empty;
            for (int ii = 0; ii <= dataGridView2.Rows.Count - 1; ii++)
            {

                DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dataGridView2.Rows[ii].Cells["Column1"];



                //buttonCell.Enabled = !(Boolean)checkCell.Value;



                //Displays the status value of the check box


                if (dataGridView2.Rows[ii].Cells["Column1"].Value == null || dataGridView2.Rows[ii].Cells["Column1"].Value.ToString() == "" || Convert.ToBoolean(dataGridView2.Rows[ii].Cells["Column1"].Value) == false)
                { }
                else
                {

                    if (dataGridView2.Rows[ii].Cells[0].Value != null)
                    { dataGridView3.Rows.Add(null,dataGridView2.Rows[ii].Cells[1].Value.ToString()); }

                    if (dataGridView2.Rows[ii].Cells[0].Value != null)
                    { dataGridView3.Rows[dataGridView3.Rows.Count + 1].Cells[1].Value = dataGridView2.Rows[ii].Cells[2].Value.ToString(); }

                    //if (dataGridView2.Rows[ii].Cells[0].Value != null || dataGridView2.Rows[ii].Cells[0].Value != "")
                    //{ dataGridView3.Rows.Add(dataGridView2.Rows[ii].Cells[0].Value.ToString()); }



                        //dataGridView3.Rows[ii].Cells[3].Value = dataGridView2.Rows[ii].Cells[3].Value.ToString();

                }
Posted
Updated 24-Apr-11 15:38pm
v2
Comments
HimanshuJoshi 24-Apr-11 21:39pm    
Added pre tag.
velvet7 25-Apr-11 1:34am    
Well, I couldn't get exactly what do you want, but check this:
dataGridView2.SelectedRows[index].Cells["cellname"].Value

This will get the selected cell's value.

1 solution

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection cn = new SqlConnection("server=LakshmiNarayana;uid=sa;pwd=cybage@123;database=narayana");
private void Form1_Load(object sender, EventArgs e)
{
SqlDataAdapter da = new SqlDataAdapter("select * from student", cn);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
DataGridViewColumn dc1 = new DataGridViewColumn(new DataGridViewTextBoxCell());
DataGridViewColumn dc2 = new DataGridViewColumn(new DataGridViewTextBoxCell());
DataGridViewColumn dc3 = new DataGridViewColumn(new DataGridViewTextBoxCell());
dataGridView2.Columns.Add(dc1);
dataGridView2.Columns.Add(dc2);
dataGridView2.Columns.Add(dc3);
}
private void button1_Click(object sender, EventArgs e)
{
dataGridView2.Rows.Clear();
string x = string.Empty;


for (int ii = 0; ii <= dataGridView1.Rows.Count - 1; ii++)
{
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[ii].Cells["Column1"];
if (dataGridView1.Rows[ii].Cells["Column1"].Value == null || dataGridView1.Rows[ii].Cells["Column1"].Value.ToString() == "" || Convert.ToBoolean(dataGridView1.Rows[ii].Cells["Column1"].Value) == false)
{
}
else
{
//if (dataGridView1.Rows[ii].Cells[0].Value != null)
//{
//dataGridView2 .ColumnAdded
dataGridView2.Rows.Add(dataGridView1.Rows[ii].Cells[1].Value.ToString(), dataGridView1.Rows[ii].Cells[2].Value.ToString(), dataGridView1.Rows[ii].Cells[3].Value.ToString());
//}
//if (dataGridView1.Rows[ii].Cells[0].Value != null)
//{
// dataGridView2.Rows[dataGridView2.Rows.Count + 1].Cells[1].Value = dataGridView1.Rows[ii].Cells[2].Value.ToString();
//}

}

}

}



}
 
Share this answer
 
Comments
LakshmiNarayana Nalluri 25-Apr-11 7:23am    
i had tried your requirement and i got it.Just paste my code see the result.ok happy coding

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900