如何将datagridview1行发送到datagridview2

时间:2018-10-08 11:12:01

标签: c#

我有两个datagridview datagridview1datagridview2datagridview1从db获取所有值,并且工作正常。我想要的是在某些情况下,我需要向datagridview1发送datagridview2

我的代码是这样的

DataTable table = new DataTable();
dataGridView1.DataSource = table;
baglantı = new SqlConnection();
baglantı.ConnectionString = 

"Server=asdsa;Database=asdsasd;User Id=asdsa;password=adsadsas";
baglantı.Open();

komut = new SqlDataAdapter("SELECT * FROM table" , baglantı);
ds = new System.Data.DataSet();
komut.Fill(ds, "table");
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.Columns[0].HeaderText = "SATICI ADI";
dataGridView1.Columns[1].HeaderText = "ALICI ADI";
dataGridView1.Columns[2].HeaderText = "URUN";
dataGridView1.Columns[3].HeaderText = "ADET";
dataGridView1.Columns[4].HeaderText = "SATICI PUANI";
dataGridView1.Columns[5].HeaderText = "ALICI PUANI";
dataGridView1.Columns[6].HeaderText = "TARIH";

dataGridView2.Columns.Add("Column", "SATICI ADI");
dataGridView2.Columns.Add("Column", "ALICI ADI");
dataGridView2.Columns.Add("Column", "URUN");
dataGridView2.Columns.Add("Column", "ADET");
dataGridView2.Columns.Add("Column", "SATICI PUANI");
dataGridView2.Columns.Add("Column", "ALICI PUANI");
dataGridView2.Columns.Add("Column", "TARIH");

int i = 0;

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    if (i != 20496)
    {
        if (dataGridView1.Rows[i].Cells[0].Value.ToString() == dataGridView1.Rows[i + 1].Cells[0].Value.ToString() && dataGridView1.Rows[i].Cells[1].Value.ToString() == dataGridView1.Rows[i + 1].Cells[1].Value.ToString() && dataGridView1.Rows[i].Cells[2].Value.ToString() == dataGridView1.Rows[i + 1].Cells[2].Value.ToString() && dataGridView1.Rows[i].Cells[3].Value.ToString() == dataGridView1.Rows[i + 1].Cells[3].Value.ToString() && dataGridView1.Rows[i].Cells[4].Value.ToString() == dataGridView1.Rows[i + 1].Cells[4].Value.ToString() && dataGridView1.Rows[i].Cells[5].Value.ToString() == dataGridView1.Rows[i + 1].Cells[5].Value.ToString() && dataGridView1.Rows[i].Cells[6].Value.ToString() == dataGridView1.Rows[i + 1].Cells[6].Value.ToString())
        {
            CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[dataGridView1.DataSource];
            currencyManager1.SuspendBinding();
            dataGridView1.Rows[i].Visible = false;
            currencyManager1.ResumeBinding();

            i = i + 1;
        }
        else
        {
            i = i + 1;
        }
    }
}

这种情况是

dataGridView1.Rows[i].Visible = false;

在这种情况下,我需要将dataGridView1.Rows [i]发送到dataGridView2.Rows

3 个答案:

答案 0 :(得分:2)

只需使用datagridView2.Rows.Add(dataGridview1.Rows[i].Cell[0].value,dataGridview1.Rows[i].Cell[1].value, ...) İt将解决您的问题

答案 1 :(得分:0)

当您说“发送”时,是否意味着要在dataGridView1dataGridView2的后面附加一行?
您是否考虑过将dataGridView2绑定到BindingList<T>可以绑定的Add()上?

答案 2 :(得分:0)

从您先前的评论中,您可以轻松地直接从数据库中获得不同或重复的值,这将更快。

此外,您的逻辑不正确,无法找到重复的值,您正在比较每个连续行的值,但是需要两个for循环