[英]loop a column in datagridview to get the value?
i am doing a project on web service and window form basically i meet some problem and not sure how to solve 我正在做一个关于Web服务和窗口形式的项目,基本上我遇到了一些问题,不确定如何解决
i want to get the data in every row in ColA of datagridview and inside into the data base through web service, to get something like this row1;row2;row3;row4......so i did a for loop but at underline with the nominal which i highlight in bold 我想在datagridview的ColA中的每一行中获取数据,并通过Web服务将其放入数据库中,以获取类似于row1; row2; row3; row4 ......之类的东西,所以我做了一个for循环,但下划线我用粗体突出的名义
private void push_Click(object sender, EventArgs e) { string nominal; for (int i = 0; i < DGV.Rows.Count; i++) { nominal = Convert.ToString(DGV.Rows[i].Cells[1].Value); } WSBrand insertInto = new WSBrand(); insertInto.Insert(**nominal**) }
this is my insert method 这是我的插入方法
[WebMethod]
public DataSet Insert(String BrandName)
{
SqlCommand dbCommand = new SqlCommand();
dbCommand.CommandText = "INSERT INTO Brand (BrandName)VALUES (@BrandName)";
dbCommand.Connection = conn;
da = new SqlDataAdapter();
da.SelectCommand = dbCommand;
dbCommand.Parameters.AddWithValue("@BrandName", BrandName);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
private void push_Click(object sender, EventArgs e)
{
StringBuilder nominal = new StringBuilder();
for(int i = 0;i<DGV.Rows.Count;i++){
nominal.Append(Convert.ToString(DGV.Rows[i].Cells[1].Value) + ";");
}
WSBrand insertInto = new WSBrand();
InsertInto.Insert(nominalList);
}
In Your WSBrand Class: 在您的WSBrand班级中:
[WebMethod]
public DataSet Insert(string nominal)
{
SqlCommand dbCommand = new SqlCommand();
dbCommand.CommandText = "INSERT INTO Brand (BrandName)VALUES (@BrandName)";
dbCommand.Connection = conn;
dbCommand.Parameters.AddWithValue("@BrandName", sb.ToString());
dbCommand.ExecuteNonQuery();
SqlCommand dbCommand2 = new SqlCommand();
dbCommand2.CommandText = "SELECT * FROM Brand";
dbCommand2.Connection = conn;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = dbCommand2;
dbCommand2.ExecuteNonQuery();
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
The problem is that nominal is being overwritten in every iteration of the for loop. 问题在于,在for循环的每次迭代中都会覆盖标称值。 If you create a List of strings that contain all the values of the DGV then it will be easier to access them all in your WSBrand class.
如果创建包含DGV所有值的字符串列表,则在WSBrand类中访问它们的所有值将更加容易。
EDIT: When you are executing the INSERT Statement the only thing that will be returned is the number of rows affected. 编辑:当您执行INSERT语句时,唯一会返回的是受影响的行数。 To get all the data back you must execute a SELECT * FROM Brands query and that will enable you to fill your Dataset with data!
要取回所有数据,您必须执行SELECT * FROM Brands查询,这将使您能够用数据填充数据集!
Final EDIT: This should work hopefully. 最终编辑:这应该可以工作。 I have changed both the push_Click method and the Insert Method.
我已经更改了push_Click方法和插入方法。 If there are any syntax errors you will have to work these out as I done this in notepad.
如果有任何语法错误,则必须像在记事本中一样进行处理。 Good Luck
祝好运
Simply use foreach loop: 只需使用foreach循环:
foreach(DataGridviewRow a in DGV.Rows)
{
string nominal = Convert.ToString(a.Cells[1].Value)
WSBrand insertInto = new WSBrand();
insertInto.Insert(nominal)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.