Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

C# Winform Rows cannot be programmatically added to the DataGridView's

$
0
0

I have 2 DataGridViews Grid 1 is populated by importing an Excel file. I am trying to copy or clone 2 columns into DataGridView2.

My error is on this line :  dataGridView2.Rows.Add(row);

It is data bound and I'm get the error ; Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.


Here's some code

 private void Form1_Load(object sender, EventArgs e)
        {               
            foreach (DataColumn dc in dt.Columns)
                cboX.Items.Add(dc.ColumnName);
            foreach (DataColumn dc in dt.Columns)
                cboY.Items.Add(dc.ColumnName);
            foreach (DataColumn dc in dt.Columns)
                cboZ.Items.Add(dc.ColumnName);


        }

 #region Data Table
        public static DataTable GetDataTableExcel(string strFileName, string Table)
        {
            System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source = " + strFileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";");
            conn.Open();
            string strQuery = "SELECT * FROM [" + Table + "]";
            System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(strQuery, conn);
            System.Data.DataSet ds = new System.Data.DataSet();
            adapter.Fill(ds);
            return ds.Tables[0];
        }



        public static string[] GetTableExcel(string strFileName)
        {
            string[] strTables = new string[100];
            Catalog oCatlog = new Catalog();
            ADOX.Table oTable = new ADOX.Table();
            ADODB.Connection oConn = new ADODB.Connection();
            oConn.Open(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strFileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";");
            oCatlog.ActiveConnection = oConn;
            if (oCatlog.Tables.Count > 0)
            {
                int item = 0;
                foreach (ADOX.Table tab in oCatlog.Tables)
                {
                    if (tab.Type == "TABLE")
                    {
                        strTables[item] = tab.Name;
                        item++;
                    }
                }
            }
            return strTables;
        }

        #endregion


 private void button4_Click(object sender, EventArgs e)
        {
                       

            // Adding columns in datagridview2
            if (dataGridView2.ColumnCount == 0)
            {
                foreach (DataGridViewColumn dgvc in dataGridView1.Columns)
                {
                    dataGridView2.Columns.Add(dgvc.Clone() as DataGridViewColumn);
                }

            }
            DataGridViewRow row = new DataGridViewRow();
            // Adding Rows in datagridview2
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {


                DataGridView dataGridView1 = new DataGridView();
                dataGridView1.ColumnCount = 5;
                dataGridView1.Columns[0].Name = "Dock id";

                row = (DataGridViewRow)dataGridView1.Rows[i].Clone();
                int intColIndex = 0;
                foreach (DataGridViewCell cell in dataGridView1.Rows[i].Cells)
                {
                    row.Cells[intColIndex].Value = cell.Value;
                    intColIndex++;
                }

                dataGridView2.Rows.Add(row);
        }


            try
            {
                string x = "";
                string y = "";
                string z = "";

                if (cboX.SelectedItem != null)
                    x = cboX.SelectedItem.ToString();
                if (cboY.SelectedItem != null)
                    y = cboY.SelectedItem.ToString();
                if (cboZ.SelectedItem != null)
                    z = cboZ.SelectedItem.ToString();

                DataTable newDt = new DataTable();
                if (y != "" && z != "")
                    newDt = PivotTable.GetInversedDataTable(dt, x, y, z, txttNullValue.Text, chkSumValues.Checked);
                else
                    newDt = PivotTable.GetInversedDataTable(dt, x, y);

                dataGridView2.DataSource = newDt;

            }
            catch (Exception err)
            {
                MessageBox.Show("Error: " + err.Message);
            }
        }

     }
}


Booney440



Viewing all articles
Browse latest Browse all 2535

Trending Articles