开发者

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

  1. i want to get the data in every row in ColA of datagridview and insid开发者_StackOverflowe 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

    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:

    [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. 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.

EDIT: When you are executing the INSERT Statement the only thing that will be returned is the number of rows affected. 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!

Final EDIT: This should work hopefully. I have changed both the push_Click method and the Insert Method. 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(DataGridviewRow a in DGV.Rows)
{
 string  nominal = Convert.ToString(a.Cells[1].Value)

    WSBrand insertInto = new WSBrand();
    insertInto.Insert(nominal)

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜