I have a gridview which is being populated at runtime(using sessions) by entering the respective values in the web form.
Now i want to retrieve all the data from that gridview and fill it into my table(MySql table)..
How can i do that???
//创建数据表
private DataTable CreateDataTable2()
{
DataTable myDataTable2 = new DataTable();
DataColumn myDataColumn;
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "Sim Imei";
myDataTable2.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "Mobile Number";
myDataTable2.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "Sim ID";
myDataTable2.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "IssuedForAccount";
myDataTable2.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "IssuedForDevice";
myDataTable2.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "Device Imei";
myDataTable2.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "Issued By";
myDataTable2.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "Invoice Number";
myDataTable2.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "Invoice Date";
myDataTable2.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "Issued To";
myDataTable2.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "Authority Name";
myDataTable2.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "Company Name";
myDataTable2.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "Company Address";
myDataTable2.Columns.Add(myDataColumn);
return myDataTable2;
}
//creating function to add data
private void AddDataToTable2(string SimImei, string MobileNumber, string SimID, string IssuedForAccount, string IssuedForDevice, string DeviceImei, string IssuedBy, string InvoiceNumber, string InvoiceDate, string IssuedTo, string AuthorityName, string CompanyName, string Address, DataTable myTable)
{
DataRow row = myTable.NewRow();
row["Sim Imei"] = SimImei;
row["Mobile Number"] = MobileNumber;
row["Sim ID"] = SimID;
row["IssuedForAccount"] = IssuedForAccount;
row["IssuedForDevice"] = IssuedForDevice;
row["Device Imei"] = DeviceImei;
row["Issued By"] = IssuedBy;
row["Invoice Number"] = InvoiceNumber;
row["Invoice Date"] = InvoiceDate;
row["Issued To"] = IssuedTo;
row["Authority Name"] = AuthorityName;
row["Company Name"] = CompanyName;
row["Company Address"] = Address;
myTable.Rows.Add(row);
}
public static void ShowAlertMessage(string error)
{
Page page = HttpContext.Current.Handler as Page;
if (page != null)
{
error = error.Replace("'", "\'");
ScriptManager.RegisterStartupScript(page, page.GetType(), "err_msg", "alert('" + error + "');", true);
}
}
点击//按钮,将值输入gridview
protected void btn_InsertSim_Click(object sender, EventArgs e)
{
reporting tableValues = new reporting();
int t = 0;
int price = 0;
try
{
#region Insert values in gridview
connection.Open();
string devicetyp = null;
string mobile = null;
try
{
string mobiledata="select MobileNumber from siminventoryin where SimImei='"+txt_SubjectIMEI.Text+"'";
MySqlCommand cmd5=new MySqlCommand (mobiledata,connection);
cmd5.ExecuteNonQuery();
MySqlDataReader rdr=cmd5.ExecuteReader();
while (rdr.Read())
{
mobile = (string)rdr["MobileNumber"];
}
rdr.Close();
string devicedata = "select Devicetype from deviceinventoryin where DeviceIMEI='" + ddl_adjoiningdeviceSerial.SelectedValue.ToString() + "'";
MySqlCommand cmd6 = new MySqlCommand(devicedata, connection);
cmd6.ExecuteNonQuery();
MySqlDataReader rd = cmd6.ExecuteReader();
while (rd.Read())
{
devicetyp = (string)rd["DeviceType"];
}
rd.Close();
AddDataToTable2(txt_SubjectIMEI.Text, mobile, txt_SubjectID.Text, ddl_accountid.SelectedValue, devicetyp, ddl_adjoiningdeviceSerial.SelectedValue, txt_issuedby.Text, txt_invoiceno.Text, txt_invoicedate.Text, txt_issuedto.Text, txt_name.Text, txt_compname.Text, txt_address.Text, (DataTable)Session["myDatatable2"]);
grd_showsim.DataSource = ((DataTable)Session["myDatatable2"]).DefaultView;
grd_showsim.DataBind();
}
catch (Exception ex)
{
ShowAlertMessage(ex.Message.ToString());
}
finally
{
}
#endregion
}
catch
{
ShowAlertMessage("Oops, Some error occured... We are working on it");
}
finally
{
connection.Close();
//txt_SubjectID.Text = "";
//txt_SubjectIMEI.Text = "";
}
}
#endregion
// gridview的数据绑定
protected void grd_showsim_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblSerial = (Label)e.Row.FindControl("lblSerial");
lblSerial.Text = i.ToString();
i++;
}
}
请帮忙...... !!
答案 0 :(得分:0)
您可以在gridview控件上运行foreach&引用每个单独的模板控件或使用Eval。像
这样的东西try{
foreach (GridViewRow gr in YourGridView.Rows)
{
TextBox tb1 = ((TextBox) gr.FindControl("YourControlId1")).Text;
TextBox tb2 = ((TextBox) gr.FindControl("YourControlId2")).Text;
// Insert to Database here
// Open Connection ; ExecuteNonQuery();
}
}
catch(Exception ex)
{
// log exception
}