我正在使用此代码在 Gridview 中动态创建行,并使用 LINQ 将这些行插入 DB。现在我想在插入数据库后返回 ID 列表,但我不知道如何。
这是我的代码:
#region [Get List Visitors]
private void GetListVisitors() {
int rowIndex = 0;
StringCollection sc = new StringCollection();
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
TextBox visitor_txtName = (TextBox)grdListVisitors.Rows[rowIndex].Cells[1].FindControl("txtName");
TextBox visitor_txtAdd = (TextBox)grdListVisitors.Rows[rowIndex].Cells[1].FindControl("txtAdd");
//ASPxComboBox visitor_cbxSex = (ASPxComboBox)grdListVisitors.Rows[rowIndex].Cells[1].FindControl("cbxSex");
sc.Add(visitor_txtName.Text + "," + visitor_txtAdd.Text);
rowIndex++;
}
//Store the current data to StringCollection
AddListVisitors(sc);
}
}
#endregion
#region [Add List Visitors]
private void AddListVisitors(StringCollection sc)
{
try {
List_Visitor add_visitors = new List_Visitor();
string[] splitItems = null;
foreach (string item in sc)
{
if (item.Contains(","))
{
Travel_DBDataContext db = new Travel_DBDataContext();
splitItems = item.Split(",".ToCharArray());
add_visitors.Fullname = splitItems[0];
add_visitors.add = splitItems[1];
db.List_Visitors.InsertOnSubmit(add_visitors);
db.SubmitChanges();
}
}
}
catch (Exception addvisitors_error) {
Response.Write(addvisitors_error.Message + "_method addlistvisitor");
}
}
#endregion