I have Form1 and Form2 and one class called Savestate
. In Form1, I have datagridview1
checkbox column and a label. The label will reflect the equipment name. The checkbox is initially empty. It’s up to the user to check or uncheck the checkboxes. Each checked checkbox will return a P while the unchecked checkbox will return an O. The P and O will be saved in dgv1p_list
that I initiated in Savestate
when user click button1
. This button1
will also save the equipment name and then go to Form2.
In Form2, there is datagridview2
which reflects the equipment name. When user click Button2
, the values in the dgv1p_list
will be exported to excel columns B to E while their respective equipment name will be exported to column A.
However, with my code below the values exported to excel are incorrect and incomplete. With reference to Checkbox , the first and last row is unchecked so therefore Column B and Column E should be an O. But with reference to Excel, the excel results are incorrect and cells are empty. Below is my code:
Class Savestate
public static List<string> dgv1p_list = new List<string>();
Form 1
public void Intcabin()
{
DataGridViewCheckBoxColumn check = new DataGridViewCheckBoxColumn();
dataGridView1.ColumnCount = 1;
dataGridView1.Columns[0].Width = 380;
dataGridView1.Columns[0].Name = "Item";
string[] row1 = new string[] { "Hello"};
string[] row2 = new string[] { "Bye"};
string[] row3 = new string[] { "okay" };
string[] row4 = new string[] { "thanks" };
object[] rows = new object[] { row1, row2, row3, row4};
foreach (string[] rowArray in rows)
{
this.dataGridView1.Rows.Add(rowArray);
}
check.HeaderText = "Status";
check.TrueValue = "P";
check.FalseValue = "O";
dataGridView1.Columns.Add(check);
}
private void button1_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow dataGridRow in dataGridView1.Rows)
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridRow.Cells[1];
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (chk != null)
{
Savestate.dgv1p_list.Add(chk.TrueValue.ToString());
}
else
{
Savestate.dgv1p_list.Add(chk.FalseValue.ToString());
}
}
}
this.Hide();
FormsCollection.Form2.Show();
}
Form 2
private void button2_Click(object sender, EventArgs e)
{
int _lastRow1 = oSheet1.Range["C" + oSheet1.Rows.Count].End[Excel.XlDirection.xlUp].Row + 1;
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
for (int j = 0; j < dataGridView2.Columns.Count; j++)
{
oSheet1.Cells[_lastRow1, 1] = dataGridView2.Rows[i].Cells[0].Value.ToString();
oSheet1.Cells[_lastRow1, j+2] = Savestate.dgv1p_list[i];
}
_lastRow1++;
}
}
Aucun commentaire:
Enregistrer un commentaire