datagrid的数据源为datatable dt
private void buttonAdd_Click(object sender, System.EventArgs e)
{
    int iCount = dt.Rows.Count;
    DataRow dtRow = dt.NewRow();
    dtRow["ID"] = ......
    ....................
    dt.Rows.InsertAt(dtRow,iCount);
    this.dataGrid.CurrentRowIndex=iCount;
}

解决方案 »

  1.   

    jackyoung02(冷雨夜) ,你这种做法需要重新绑定一遍datatable,有没有更直接和高效的办法?关注中
      

  2.   

    SqlConnection myConnection = new SqlConnection(ConnectionString);
                SqlDataAdapter myCommand = new SqlDataAdapter(SelectCommand, myConnection);
        
                DataSet ds = new DataSet();
                myCommand.Fill(ds);
        
                // 看你有多少字段了,这里演示了三个,类型也是varchar(也就是string)
                object[] rowValues = { "", "", "" };
                ds.Tables[0].Rows.Add(rowValues);
      

  3.   

    先在datatable里增加一个空白行,然后绑定datagrid
    private void Button1_Click(object sender, System.EventArgs e)
    {

    sqlConnection1.Open();
    DataSet objDataset = new DataSet();
    sqlDataAdapter1.Fill(objDataset, "address1");
    rowValues ={"","",""};
    objDataset.Tables["address1"].Rows.Add(rowValues);
    DataGrid1.DataSource=objDataset.Tables["address1"].DefaultView;
    DataGrid1.DataBind();
    sqlConnection1.Close();

    }如果想使该行可编辑,在指定数据源前加入
    count = objDataset.Tables["address1"].Rows.Count;
    DataGrid1.EditItemIndex = count-1;
      

  4.   

    SqlConnection myConnection = new SqlConnection(ConnectionString);
                SqlDataAdapter myCommand = new SqlDataAdapter(SelectCommand, myConnection);
        
                DataSet ds = new DataSet();
                myCommand.Fill(ds);            object[] rowValues = { "", "", "" };
                ds.Tables[0].Rows.Add(rowValues);            int recordCount = ds.Tables[0].Rows.Count;
                if (recordCount > 1)
                    recordCount--;
                DataGrid1.CurrentPageIndex = recordCount/dgUserInfo.PageSize;
                DataGrid1.EditItemIndex = recordCount%dgUserInfo.PageSize;            DataGrid1.DataSource = ds;
                DataGrid1.DataBind();