我有数据表“dt”,其中第二列有名称,我将其值插入datagridview“dgvbat”..现在我想通过使用另一个数据表“dt1”将值插入“dgvbat”的第4列。 当我试图在第5列插入值之前的第2列值被删除..
DataTable dt = new DataTable();
dt.Columns.Add("SLNO", Type.GetType("System.String"));
dt.Columns.Add("Batsman", Type.GetType("System.String"));
dt.Columns.Add("Dismissal", Type.GetType("System.String"));
dt.Columns.Add("Bowler", Type.GetType("System.String"));
dt.Columns.Add("Runs", Type.GetType("System.String"));
dt.Columns.Add("Balls", Type.GetType("System.String"));
dt.Columns.Add("Mins", Type.GetType("System.String"));
dt.Columns.Add("Fours", Type.GetType("System.String"));
dt.Columns.Add("Sixes", Type.GetType("System.String"));
dt.Columns.Add("SR", Type.GetType("System.String"));
int count = 1;
for (int i = 0; i <= (team1.Count - 1); i++)
{
if (captain1 == keeper1)
{
if (team1[i] == captain1)
{
DataRow dr = dt.NewRow();
dr.BeginEdit();
dr["SLNO"] = count;
count++;
dr["Batsman"] = team1[i];
dr.EndEdit();
dt.Rows.Add(dr);
dt.AcceptChanges();
}
else
{
DataRow dr = dt.NewRow();
dr.BeginEdit();
dr["SLNO"] = count;
count++;
dr["Batsman"] = team1[i];
dr.EndEdit();
dt.Rows.Add(dr);
dt.AcceptChanges();
}
}
这是我的代码插入第5列
DataTable dt1 = new DataTable();
dt1.Columns.Add("SLNO", Type.GetType("System.String"));
dt1.Columns.Add("Batsman", Type.GetType("System.String"));
dt1.Columns.Add("Dismissal", Type.GetType("System.String"));
dt1.Columns.Add("Bowler", Type.GetType("System.String"));
dt1.Columns.Add("Runs", Type.GetType("System.String"));
dt1.Columns.Add("Balls", Type.GetType("System.String"));
dt1.Columns.Add("Mins", Type.GetType("System.String"));
dt1.Columns.Add("Fours", Type.GetType("System.String"));
dt1.Columns.Add("Sixes", Type.GetType("System.String"));
dt1.Columns.Add("SR", Type.GetType("System.String"));
for (int m = 0; m <= (teamA.Count - 1); m++)
{
if (!DBNull.Value.Equals((dgvbat.Items[m] as DataRowView).Row.ItemArray[1]) && ((dgvbat.Items[m] as DataRowView).Row.ItemArray[1]).ToString() != "")
{
if(cbxbatsmen.SelectedValue.ToString() == ((dgvbat.Items[m] as DataRowView).Row.ItemArray[1]))
{
Runs1 = Runs1 + i;
DataRow dr = dt1.NewRow();
dr.BeginEdit();
dr["Runs"] = Runs1;
dr.EndEdit();
dt1.Rows.Add(dr);
dt1.AcceptChanges();
}
}
}
dgvbat.DataContext = dt1;
谢谢你......