如何选择datagridview行并传输其他datagridview [英] How to select datagridview row and transfer other datagridview

查看:90
本文介绍了如何选择datagridview行并传输其他datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

在我的datagridview中,所有列都有复选框和用户检查是否有任何一个选定的行。我有一个按钮当用户点击按钮我想传输哪些选择的行/行如何解决这个问题?

所有人都是Thaxs :)





Hello,
In my datagridview all columns have checkbox and user checked if any one selected row.I have a button when user click button I want to transfer which selected row/rows how can I solve this problem?
Thaxs for all :)


Int32 ID = Convert.ToInt32(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value.ToString());
            MessageBox.Show("ID" + ID);
            DataTable table = new DataTable();
            using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Dorana.mdf;Integrated Security=True;User Instance=True;Pooling=False"))
            {
                string SQL = @"Select * from rezerve Where ( rezerveID = @rzveID);";
                SqlCommand cmd = new SqlCommand(SQL, conn);
                cmd.Parameters.AddWithValue("@rzveID", ID);

                conn.Open();
                table.Load(cmd.ExecuteReader());
                conn.Close();
            }
            rzve rzrv= new rzve();
            // DataGridView dgv1 = new DataGridView { Dock = DockStyle.Fill };
            rzrv.Controls.Add(dataGridView1);
            rzrv.dataGridView1.DataSource = table;
             rzrv.Show();
           
            rzrv.dataGridView1.Columns["name"].HeaderText = "Name";
            rzrv.dataGridView1.Columns["surname"].HeaderText = "Surname";
            rzrv.dataGridView1.Columns["gender"].HeaderText = "Gender";
            rzrv.dataGridView1.Columns["date"].HeaderText = "Date";

推荐答案

我的项目使用datagridviews。用户可以使用行标题选择行,而不是使用复选框(Ctrl + Shift用于多选)。



My Project uses datagridviews. Instead of using checkboxes the user can select rows using the row headers (Ctrl+Shift for multi-select).

this.dataGridView1.SelectionChanged += new EventHandler(dataGridViewDevices_SelectionChanged);


void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
            DataGridViewSelectedRowCollection rows = this.dataGridViewDevices.SelectedRows;
            int n = rows.Count;

//copy items

for (int i = 0; i < n; i++)
{
object item = rows[i].Cells[0].Value; //get cells by row index or Column Name
//copy to new grid or datatable bound to other grid                      
}

}


protected void btnClick(object sender, EventArgs e) {
string ids = String.Empty;
    foreach (DataRowView drv in DataGridView1.Rows) {
        if (drv[0].Checked) { // <-- you might have to cast here
ids += drv["rezerveID "];
        }
            // your query, ids goes into @rzveID placeWHERE rezerve_id IN @rzveID

    }
}







您可以考虑在CheckBoxCellClick上保留已检查的行索引集合(使用CellContentClick或checkedChange或其他一些)。在这种情况下,您不会像上面的代码那样遍历网格中的所有行,但是您要引入至少一个对象(集合),事件(checkedChanged)以及用于添加和删除项目的逻辑。



我会选择更简单的解决方案。



如果这有帮助请花点时间接受解决方案。谢谢。




You might consider keeping checked row indexes collection on CheckBoxCellClick (use CellContentClick or checkedChange or some such). In that case you're not iterating through all rows in the grid like the above code, but you're introducing at least one more object (collection), event (checkedChanged) and logic for adding and removing the items.

I would go with simpler solution.

If this helps please take time to accept the solution. Thank you.


这篇关于如何选择datagridview行并传输其他datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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