1
votes

I have little problem , I shared images and code below.

I have two datagridview in my code block. First(left side in form) datagridview's name searchDataGridView. This grid shows data from my database by some filters. Second(right side in form) datagridview's name dataGridView1. This grid shows when I click next button if I select some rows from first grid then this rows will add to dataGridView1. This code block working very well but this that my problem when I click next button and selected rows and added to dataGridView1 then searchDataGridVİew will show new items and selected rows will add to datagridView1, but when I add new items to dataGridView older rows will clear and new selected rows will add but I need that news selected rows must add to under older rows. Thank you for helping.

 private void buttonNext_Click(object sender, EventArgs e)
        {

           
            Search searchObj = new Search();
            DataTable dt = new DataTable();
            dt.Columns.Add("Item");
            dt.Columns.Add("Company");
            dt.Columns.Add("Category");
            dt.Columns.Add("Price");

            foreach (DataGridViewRow drv in searchDataGridView.Rows)
            {
                bool chkboxselect = Convert.ToBoolean(drv.Cells["CheckBox"].Value);             

                if (chkboxselect)
                {
                    dt.Rows.Add(drv.Cells[2].Value, drv.Cells[3].Value, drv.Cells[4].Value, drv.Cells[8].Value);
                    drv.DefaultCellStyle.BackColor = Color.Gray;
                    drv.DefaultCellStyle.ForeColor = Color.Aqua;

                }
              
                dataGridView1.DataSource = dt;            
                

            }
            

            counter +=1;
            

            if (counter == maxIndex)
            {
                counter =0;
            }

            try
            {
                searchCategoryComboBox.SelectedIndex = counter;
            }


            catch (System.ArgumentOutOfRangeException)  // CS0168
            {
                MessageBox.Show("hata yakalandı");
                return;
            }

            //catch for error outofrange
           
          
            searchCategoryComboBox.SelectedIndex =counter;
            searchObj.Company = searchCompanyComboBox.Text;
            searchObj.Category = searchCategoryComboBox.Text;


            SearchManager searchMangObj = new SearchManager();
            DataTable dt2 = searchMangObj.SearchInfo(searchObj);
            searchDataGridView.DataSource = dt2;



            textBox1.Text = String.Empty;



        }

enter image description here

enter image description here

1
Okey. I erased.Recep Enes Çevlik

1 Answers

1
votes

The problem is the way you get data from the db:

        DataTable dt2 = searchMangObj.SearchInfo(searchObj);
        searchDataGridView.DataSource = dt2;

Your SearchInfo method returns a new datatable and you bind that so the rows from the previous table are lost. Modify this method so it takes a datatable as a parameter (ie the existing datatable) and add rows to it.

You didn't say how your SearchInfo works to get rows from the db, but I suspect it will use a dataadapter or tableadapter, in which case you should set its ClearBeforeFill property to false to stop it from clearing the search datatable (the table that is databind'd to the searchdatagridview.DataSource)

You would then use it like:

searchMangObj.SearchInfo(searchDataGridView.DataSource as DataTable);

And it would be coded like:

void SearchInfo(DataTable dt){
  var dataadapter = blah blah blah;
  dataadapater.ClearBeforeFill = false;
  dataadapter.Fill(dt);
}