chairmanPC
Active member
- Joined
- Apr 19, 2021
- Messages
- 38
- Programming Experience
- 10+
So here's the problem: I am trying to copy and paste data from one DataGridView to another. First DGV is called allData and second DGV is called selectedDGV. There are 8 columns in allData while there are 5 in selectedDGV.
As long as the program reads data with more than 8 columns, it's fine. If there is less than 8 columns, then of course I get NullReferenceException.
Codes below
And this is the code where I am supposed to copy selected rows.
For some odd reason, the program would not catch any exceptions for btnAdd function. Did I surround the try/catch in the wrong place?
As long as the program reads data with more than 8 columns, it's fine. If there is less than 8 columns, then of course I get NullReferenceException.
Codes below
Open FIle code:
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(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);
}
}
}catch(Exception nani)
{
ErrorForm error = new ErrorForm();
error.ShowDialog();
error.label3.Text = nani.Message;
}
}
And this is the code where I am supposed to copy selected rows.
C#:
private void btnAdd_Click(object sender, EventArgs e)
{
selectedDGV.Rows.Clear();
for (int i = 0; i < allData.Rows.Count; i++)
{
try
{
if (Convert.ToBoolean(allData.Rows[i].Cells[8].Value) == true)
{
int n = selectedDGV.Rows.Add();
selectedDGV.Rows[n].Cells[0].Value = allData.Rows[i].Cells[1].Value.ToString();
selectedDGV.Rows[n].Cells[1].Value = allData.Rows[i].Cells[3].Value.ToString();
selectedDGV.Rows[n].Cells[2].Value = allData.Rows[i].Cells[4].Value.ToString();
selectedDGV.Rows[n].Cells[3].Value = allData.Rows[i].Cells[5].Value.ToString();
selectedDGV.Rows[n].Cells[4].Value = allData.Rows[i].Cells[6].Value.ToString();
selectedDGV.Rows[n].Cells[5].Value = allData.Rows[i].Cells[7].Value.ToString();
}
}
catch (Exception nani)
{
ErrorForm error = new ErrorForm();
error.label3.Text = nani.Message;
error.ShowDialog();
}
}
}
For some odd reason, the program would not catch any exceptions for btnAdd function. Did I surround the try/catch in the wrong place?
Last edited: