NullReferenceException when trying to copy DataGridView

chairmanPC

Active member
Joined
Apr 19, 2021
Messages
29
Programming Experience
10+
I am trying to get my program in C# to copy and paste a selected row from one DataGridView to another DataGridView. Column 0 is the checkbox cell. The selected rows will be copied into the second DGV. But when I was trying to make this happen, I get NullReferenceException in this line: if ((bool)item.Cells[0].Value == true)

Here is what I did:

C#:
        private void btnAdd_Click(object sender, EventArgs e)
    {
        selectedDGV.Rows.Clear();
        foreach (DataGridViewRow item in allData.Rows)
        {
            if ((bool)item.Cells[0].Value == true)
            {
                int n = selectedDGV.Rows.Add();
                selectedDGV.Rows[n].Cells[0].Value = item.Cells[3].Value.ToString();
                selectedDGV.Rows[n].Cells[1].Value = item.Cells[4].Value.ToString();
                selectedDGV.Rows[n].Cells[2].Value = item.Cells[5].Value.ToString();
                selectedDGV.Rows[n].Cells[3].Value = item.Cells[6].Value.ToString();
                selectedDGV.Rows[n].Cells[4].Value = item.Cells[7].Value.ToString();
                selectedDGV.Rows[n].Cells[5].Value = item.Cells[8].Value.ToString();
            }
        }
    }


Here is my code to browse for file: `
C#:
private void button2_Click(object sender, EventArgs e) { try { //updateDataBtn.Enabled = true; selectedDGV.Rows.Clear(); importExcelToDataGridViewApp = new Microsoft.Office.Interop.Excel.Application();
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Browse for Excel File";
            ofd.Filter = "Excel and CSV Files|*.xlsx;*.csv";
            
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = ofd.FileName;
                importExcelToDataGridViewWorkbook = importExcelToDataGridViewApp.Workbooks.Open(ofd.FileName);
                importExcelToDataGridViewWorksheet = importExcelToDataGridViewWorkbook.ActiveSheet;
                importExcelToDataGridViewRange = importExcelToDataGridViewWorksheet.UsedRange;


                for (int rowIndex = 2; rowIndex <= importExcelToDataGridViewRange.Rows.Count; rowIndex++)
                {
                    allData.Rows.Add(null,importExcelToDataGridViewWorksheet.Cells[rowIndex, 1].Value,
                                            importExcelToDataGridViewWorksheet.Cells[rowIndex, 2].Value,
                                            importExcelToDataGridViewWorksheet.Cells[rowIndex, 3].Value,
                                             importExcelToDataGridViewWorksheet.Cells[rowIndex, 6].Value,
                                            importExcelToDataGridViewWorksheet.Cells[rowIndex, 7].Value,
                                             importExcelToDataGridViewWorksheet.Cells[rowIndex, 8].Value,
                                             importExcelToDataGridViewWorksheet.Cells[rowIndex, 9].Value,
                                             importExcelToDataGridViewWorksheet.Cells[rowIndex, 10].Value,
                                            null);
                }
            }
        }catch(Exception eee)
        {
            ErrorForm error= new ErrorForm();
            error.ShowDialog();
            error.label3.Text = eee.Message;
        }
    }


The data in the DGV's come from an excel file. How can I fix this problem?
 
Last edited by a moderator:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,555
Location
Chesapeake, VA
Programming Experience
10+
Moved to WinForms and fixed code tags.

@chairmanPC : please post your questions to the appropriate subforum. Lately most of your questions have been about WinForms, not general VS.NET or C# programming, yet you continue to do so. I'd you are not sure what forum to post in, leave a note at the bottom to ask for it to be moved to a more appropriate forum.
 

NoUserHere

Well-known member
Joined
Sep 5, 2018
Messages
2,138
Programming Experience
10+
Debug your code, and put a break point on this line line 6 and after on int n = selectedDGV.Rows.Add();. You may find you have two problems.

Press f11 to step through your code and check at any point is n null?

I don't think you want to iterate over all of the rows. Shouldn't you be iterating over only those that are selected?

Shouldn't you be adding values in the add method?
 
Last edited:

NoUserHere

Well-known member
Joined
Sep 5, 2018
Messages
2,138
Programming Experience
10+
Look what happens when you add rows like you are, and look what also happens when you don't have a value set on a given cell.
Screenshot_40.jpg


What do you think the problem is?
 
Top Bottom