C# .NET - Rows cannot be programmatically added to the DataGridView's rows collection when the contr

Asked By kavitha varma on 19-Jun-12 11:53 PM
 void MainForm_Load(object sender, EventArgs e)
        {
            #region DataBinding
            
            dataGridView1.AutoGenerateColumns = false;
            // Create the list of CallItem objects which will supply data to the DataGridView.
            source = new SyncBindingList<CallItem>(SynchronizationContext.Current);
            source.AddingNew += (sender1, e1) => e1.NewObject = new CallItem();
            dataGridView1.DataSource = source;
              }

   private void btnImport_click_Click(object sender, EventArgs e)
        {
            DialogResult result1 = MessageBox.Show("Would you like to save changes?", "Save", MessageBoxButtons.YesNo);

            SaveFileDialog sv = new SaveFileDialog();
            DialogResult svResult = sv.ShowDialog();

            if (svResult == DialogResult.Cancel)
            {
                OpenFileDialog dlg = new OpenFileDialog();



                DialogResult dlgResult = dlg.ShowDialog();

                if (dlgResult == DialogResult.OK)
                {
                    txtPath.Text = dlg.FileName;
                }
            }
            
            string rowValue;
            string[] cellValue;
            //dataGridView1.AutoGenerateColumns = false;
            if (System.IO.File.Exists(txtPath.Text))
            {
                System.IO.StreamReader streamReader = new StreamReader(txtPath.Text);

                // Reading header
                rowValue = streamReader.ReadLine();
                cellValue = rowValue.Split(';');
                for (int i = 0; i <= cellValue.Count() - 1; i++)
                {
                    DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
                    column.Name = cellValue[i];
                    column.HeaderText = cellValue[i];
                    //dataGridView1.Columns.Add(column);
                }

                // Reading content
                while (streamReader.Peek() != -1)
                {
                    rowValue = streamReader.ReadLine();
                    cellValue = rowValue.Split(';');
                    dataGridView1.Rows.Add(cellValue);
                    
                }

                streamReader.Close();
            }
        }


How can i Rebind the data to the list item
dipa ahuja replied to kavitha varma on 20-Jun-12 04:49 AM
There are two ways to bind data.

1. bind using database [not allowed to insert rows programatically to datagridview]

2.bind with local datatable allow to add new rows using datatable
dipa ahuja replied to kavitha varma on 20-Jun-12 04:49 AM
There are two ways to bind data.

1. bind using database [not allowed to insert rows programatically to datagridview]

2.bind with local datatable allow to add new rows using datatable