private void Button2_Click(object sender, System.EventArgs e)//保存添加的行
{
try
{
int i=DataGrid1.Items .Count;
string pr_oduct_id=((TextBox)(DataGrid1.Items[i-1].Controls[2].Controls[0])).Text;//在Button的Click事件中,
string pr_oductname=((TextBox)(DataGrid1.Items[i-1].Controls[3].Controls[0])).Text;//只能使用这个方法得到DataGrid
string pr_ovider=((TextBox)(DataGrid1.Items[i-1].Controls[4].Controls[0])).Text;//中的新行的数据,所以要记住方法的使用
string pr_ice=((TextBox)(DataGrid1.Items[i-1].Controls[5].Controls[0])).Text;
string un_it=((TextBox)(DataGrid1.Items[i-1].Controls[6].Controls[0])).Text;
string qt_y=((TextBox)(DataGrid1.Items[i-1].Controls[7].Controls[0])).Text;
string st_ock_date=((TextBox)(DataGrid1.Items[i-1].Controls[8].Controls[0])).Text;
if(pr_oductname!="" && pr_ovider!=""&& pr_ice!=""&& un_it!=""&& qt_y!="" && st_ock_date!="")
{
SqlConnection connInsert = new SqlConnection(strCnn);
connInsert.Open();
String sql_edit = "insert into T_TRA_PRODUCT values('"+pr_oduct_id+"','"+pr_oductname+"','"+pr_ovider+"','"+pr_ice+"','"+un_it+"','"+qt_y+"','"+st_ock_date+"')";
SqlCommand sqlCommandUpdate = new SqlCommand(sql_edit,connInsert);
sqlCommandUpdate.ExecuteNonQuery();
connInsert.Close();
sql = "Select * FROM T_TRA_PRODUCT";
DataGrid1.EditItemIndex = -1;
DataGrid1.DataSource = CreateDataSource();
DataGrid1.DataBind();
}
}
catch(Exception ee)
{
}
}