Data Gridview选择所有值 [英] Data Gridview select all the values
本文介绍了Data Gridview选择所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨朋友..,
我使用我的代码将datagridview中的所有值存储到我的服务器,但这里我的错误是
无法在数据绑定的DataGridView控件上设置ColumnCount属性。在我的例外请给我一个想法,我会使用dis代码..
Hi Friends..,
I use my code to store all the values in the datagridview to my server but here am geting my error as
"ColumnCount property cannot be set on a data-bound DataGridView control." in my exception pls give me an idea hw will i use dis code..
try
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
value1 = row.Cells[0].Value.ToString();
value2 = row.Cells[1].Value.ToString();
value3 = row.Cells[2].Value.ToString();
value4 = row.Cells[3].Value.ToString();
value5 = row.Cells[4].Value.ToString();
dataGridView1.ColumnCount = 5;
dataGridView1.Columns[0].Name = "Age";
dataGridView1.Columns[1].Name = "workclass";
dataGridView1.Columns[2].Name = "fnlwgt";
dataGridView1.Columns[3].Name = "education";
dataGridView1.Columns[4].Name = "maritalstatus";
string[] row1 = new string[] { value1.ToString(), value2.ToString(), value3.ToString(), value4.ToString(), value5.ToString() };
dataGridView1.Rows.Add(row1);
//...
cmd = new SqlCommand("insert into rule1 values ('" + value1.ToString() + "','" + value2.ToString() + "','" + value3.ToString() + "','" + value4.ToString() + "','" + value5.ToString() + "')", cn.con);
cmd.ExecuteNonQuery();
cmd.Dispose();
MessageBox.Show("Execute Process!!");
}
}
}
catch (Exception error)
{
}
谢谢...
Thank You...
推荐答案
尝试以下编码(按照你的项目编辑)
Try the below coding(edit this as per your project)
protected void Button1_Click1(object sender, EventArgs e)
{
string a = Request.QueryString["var"];
// TextBox1.Text = "";
foreach (GridViewRow gvr in GridView1.Rows)
{
if (gvr.RowType == DataControlRowType.DataRow)
{
CheckBox cb = (CheckBox)(gvr.FindControl("CheckBox1"));
if (cb.Checked == true)
{ SqlConnection cn = new SqlConnection("Data Source=FABSYSXX-PC; Initial Catalog=rule; User Id=sa; Pwd=saadmin");
cn.Open();
SqlCommand cmd = new SqlCommand("Insert into Trans values('" + Session["count"].ToString() + "','" + gvr.Cells[1].Text + "','" + gvr.Cells[2].Text + "','" + gvr.Cells[3].Text + "','" + a + "')", cn); //Write Your Query to insert into database table
int iCount = cmd.ExecuteNonQuery();
cn.Close();
//TextBox1.Text = TextBox1.Text + "," + gvr.Cells[1].Text;
// TextBox2.Text = TextBox2.Text + "," + gvr.Cells[2].Text;
}
}
}
Response.Write("<SCRIPT>alert('Transcation completed Successfully!!!! ')</SCRIPT>");
//T
这篇关于Data Gridview选择所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文