各位大侠,有那位做过用aspxgrid连续添加新行,请不吝赐教,
我用newrow进行添加,添加完成以后,我还想添加新行,可是不能保存以前添加的行的内容呀,就是不能继续添加呀,
------解决方案--------------------当你修改完时调用它的.AcceptChanges()方法。!
------解决方案--------------------protected DataTable GetDataFromGrid()
{
DataTable dt1 = new DataTable( "Table1 ");
dt1.Columns.Add( "ID ");
dt1.Columns.Add( "Company ");
dt1.Columns.Add( "ItemNo ");
dt1.Columns.Add( "PartNo ");
dt1.Columns.Add( "Condition ");
dt1.Columns.Add( "Qty ");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow gRow = GridView1.Rows[i];
DataRow newRow = dt1.NewRow();
newRow[0] = i + 1;
newRow[1] = ((ComboBox)gRow.FindControl( "cbCompany ")).Text;
newRow[2] = ((TextBox)gRow.FindControl( "txtItemNo ")).Text;
newRow[3] = ((TextBox)gRow.FindControl( "txtPartNo ")).Text;
newRow[4] = ((DropDownList)gRow.FindControl( "DropDownList1 ")).SelectedValue;
newRow[5] = ((TextBox)gRow.FindControl( "txtQty ")).Text;
dt1.Rows.Add(newRow);
}
dt1.AcceptChanges();
return dt1;
}
protected void btnInsertRow_Click(object sender, EventArgs e)
{
DataTable dt = this.GetDataFromGrid();
DataRow newRow = dt.NewRow();
newRow[ "ID "] = dt.Rows.Count + 1;
newRow[ "Company "] = " ";
newRow[ "ItemNo "] = " ";
newRow[ "PartNo "] = " ";
newRow[ "Condition "] = "New ";
newRow[ "Qty "] = "1 ";
dt.Rows.Add(newRow);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
ComboBox cb = this.GridView1.Rows[i].FindControl( "cbCompany ") as ComboBox;
cb.Text = dt.Rows[i][ "Company "].ToString();
}
dt.Dispose();
}
------解决方案--------------------//单选添加按钮
protected void ibtnAddOption_Click(object sender, ImageClickEventArgs e)
{
if (this.txtOption.Text.Trim() != " ")
{
if (this.txtNum.Text == "0 ")//初始化Session
{
ExamOptionBLL Option = new ExamOptionBLL();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
if (Request.QueryString[ "QuestID "] != null)
{
ds = Option.GetList( "ExamQuestID= " + this.txtQuestID.Text);
}
else
{
ds = Option.GetList( "OptionID=-1 ");
}
dt = ds.Tables[0];
DataRow row = dt.NewRow();
row[ "OptionName "] = this.txtOption.Text.Trim();
dt.Rows.Add(row);
Session[ "Option "] = dt;
this.