我想從第一個datagridview複製所有行到第二個,但我不想從第一個datagridview的空行,我需要重新格式化第二個datagridview的佈局。這使我瘋狂 - 需要幫助。選擇所有datagridview行到另一個gridview
private void btnConvert_Click(object sender, EventArgs e)
{
dgvData.SelectAll();
DataTable dt = new DataTable();
DataColumn dc;
DataRow dr;
//build cols
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "DS";
dc.Unique = false;
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "GLAccount";
dc.Unique = false;
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "SubAccount";
dc.Unique = false;
dt.Columns.Add(dc);
dc = new DataColumn();
//dc.DataType = System.Type.GetType("System.DateTime");
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Date";
dc.Unique = false;
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Description";
dc.Unique = false;
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.Decimal");
dc.ColumnName = "Amount";
dc.Unique = false;
dt.Columns.Add(dc);
//build rows
foreach (DataGridViewRow row in this.dgvData.SelectedRows)
{
//how do i prevent copying over the empty row which happens to be the last row in the first datagridview?
int i;
i = dgvData.SelectedCells[0].RowIndex; //should this start with the first row?
int month = 12;
DateTime y = DateTime.Now;
int year = y.Year;
for (int ir = 1; ir <= month; ir++)
{
dr = dt.NewRow();
dr["DS"] = dgvData.Rows[i].Cells[0].Value.ToString();
dr["GLAccount"] = dgvData.Rows[i].Cells[1].Value.ToString();
dr["SubAccount"] = dgvData.Rows[i].Cells[2].Value.ToString();
if (ir == 7)
{
dr["Date"] = "1/01/" + (year + 1).ToString();
}
else if (ir == 8)
{
dr["Date"] = "2/01/" + (year + 1).ToString();
}
else if (ir == 9)
{
dr["Date"] = "3/01/" + (year + 1).ToString();
}
else if (ir == 10)
{
dr["Date"] = "4/01/" + (year + 1).ToString();
}
else if (ir == 11)
{
dr["Date"] = "5/01/" + (year + 1).ToString();
}
else if (ir == 12)
{
dr["Date"] = "6/01/" + (year + 1).ToString();
}
else if (ir == 1)
{
dr["Date"] = "7/01/" + year.ToString();
}
else if (ir == 2)
{
dr["Date"] = "8/01/" + year.ToString();
}
else if (ir == 3)
{
dr["Date"] = "9/01/" + year.ToString();
}
else if (ir == 4)
{
dr["Date"] = "10/01/" + year.ToString();
}
else if (ir == 5)
{
dr["Date"] = "11/01/" + year.ToString();
}
else
{
dr["Date"] = "12/01/" + year.ToString();
}
dr["Description"] = dgvData.Rows[i].Cells[3].Value.ToString();
if (ir == 7)
{
if (dgvData.Rows[i].Cells[10].Value == null || dgvData.Rows[i].Cells[10].Value == "" || dgvData.Rows[i].Cells[10].Value == DBNull.Value)
{
dr["Amount"] = Convert.ToDecimal(0);
}
else
{
dr["Amount"] = Convert.ToDecimal(dgvData.Rows[i].Cells[10].Value);
}
}
else if (ir == 8)
{
if (dgvData.Rows[i].Cells[11].Value == null || dgvData.Rows[i].Cells[11].Value == "" || dgvData.Rows[i].Cells[11].Value == DBNull.Value)
{
dr["Amount"] = Convert.ToDecimal(0);
}
else
{
dr["Amount"] = Convert.ToDecimal(dgvData.Rows[i].Cells[11].Value);
}
}
else if (ir == 9)
{
if (dgvData.Rows[i].Cells[12].Value == null || dgvData.Rows[i].Cells[12].Value == "" || dgvData.Rows[i].Cells[12].Value == DBNull.Value)
{
dr["Amount"] = Convert.ToDecimal(0);
}
else
{
dr["Amount"] = Convert.ToDecimal(dgvData.Rows[i].Cells[12].Value);
}
}
else if (ir == 10)
{
if (dgvData.Rows[i].Cells[13].Value == null || dgvData.Rows[i].Cells[13].Value == "" || dgvData.Rows[i].Cells[13].Value == DBNull.Value)
{
dr["Amount"] = Convert.ToDecimal(0);
}
else
{
dr["Amount"] = Convert.ToDecimal(dgvData.Rows[i].Cells[13].Value);
}
}
else if (ir == 11)
{
if (dgvData.Rows[i].Cells[14].Value == null || dgvData.Rows[i].Cells[14].Value == "" || dgvData.Rows[i].Cells[14].Value == DBNull.Value)
{
dr["Amount"] = Convert.ToDecimal(0);
}
else
{
dr["Amount"] = Convert.ToDecimal(dgvData.Rows[i].Cells[14].Value);
}
}
else if (ir == 12)
{
if (dgvData.Rows[i].Cells[15].Value == null || dgvData.Rows[i].Cells[15].Value == "" || dgvData.Rows[i].Cells[15].Value == DBNull.Value)
{
dr["Amount"] = Convert.ToDecimal(0);
}
else
{
dr["Amount"] = Convert.ToDecimal(dgvData.Rows[i].Cells[15].Value);
}
}
else if (ir == 1)
{
if (dgvData.Rows[i].Cells[4].Value == null || dgvData.Rows[i].Cells[4].Value == "" || dgvData.Rows[i].Cells[4].Value == DBNull.Value)
{
dr["Amount"] = Convert.ToDecimal(0);
}
else
{
dr["Amount"] = Convert.ToDecimal(dgvData.Rows[i].Cells[4].Value);
}
}
else if (ir == 2)
{
if (dgvData.Rows[i].Cells[5].Value == null || dgvData.Rows[i].Cells[5].Value == "" || dgvData.Rows[i].Cells[5].Value == DBNull.Value)
{
dr["Amount"] = Convert.ToDecimal(0);
}
else
{
dr["Amount"] = Convert.ToDecimal(dgvData.Rows[i].Cells[5].Value);
}
}
else if (ir == 3)
{
if (dgvData.Rows[i].Cells[6].Value == null || dgvData.Rows[i].Cells[6].Value == "" || dgvData.Rows[i].Cells[6].Value == DBNull.Value)
{
dr["Amount"] = Convert.ToDecimal(0);
}
else
{
dr["Amount"] = Convert.ToDecimal(dgvData.Rows[i].Cells[6].Value);
}
}
else if (ir == 4)
{
if (dgvData.Rows[i].Cells[7].Value == null || dgvData.Rows[i].Cells[7].Value == "" || dgvData.Rows[i].Cells[7].Value == DBNull.Value)
{
dr["Amount"] = Convert.ToDecimal(0);
}
else
{
dr["Amount"] = Convert.ToDecimal(dgvData.Rows[i].Cells[7].Value);
}
}
else if (ir == 5)
{
if (dgvData.Rows[i].Cells[8].Value == null || dgvData.Rows[i].Cells[8].Value == "" || dgvData.Rows[i].Cells[8].Value == DBNull.Value)
{
dr["Amount"] = Convert.ToDecimal(0);
}
else
{
dr["Amount"] = Convert.ToDecimal(dgvData.Rows[i].Cells[8].Value);
}
}
else
{
if (dgvData.Rows[i].Cells[9].Value == null || dgvData.Rows[i].Cells[9].Value == "" || dgvData.Rows[i].Cells[9].Value == DBNull.Value)
{
dr["Amount"] = Convert.ToDecimal(0);
}
else
{
dr["Amount"] = Convert.ToDecimal(dgvData.Rows[i].Cells[9].Value);
}
}
dt.Rows.Add(dr);
}
}
//bind
dgvConverted.DataSource = dt;
}
Excel如何與此相關? – 2012-08-15 14:10:07