Linojan
Linojan

Reputation: 21

How to add rows to Datagridview from class?

I have a win form and it has one datagridview and button. When i press that button it calls viewOrderHistory() method from Order class. From there i want to add rows to datagridview. My form name orderHistory. This is my viewOrderHistory method.

OrderHistory OH = new OrderHistory();
public void viewOrderHistory(int id){
DataGridView orderHistoryProductDataGrid = OH.dataGridView2;
try
{
int count = 0;
orderHistoryProductDataGrid.Rows.Clear();
orderHistoryProductDataGrid.Refresh();
DatabaseConnection();//Database connection
string ord = "SELECT * FROM Orders_Products WHERE ID='" + id + "'";
SqlCommand ordPro = new SqlCommand(ord, myCon);
SqlDataReader rdr = ordPro.ExecuteReader();
while (rdr.Read())
{
DataGridViewRow row = (DataGridViewRow)orderHistoryProductDataGrid.Rows[count].Clone();
row.Cells[0].Value = rdr["Code"].ToString();
row.Cells[1].Value = mainform.getProduct(rdr["Code"].ToString());
row.Cells[2].Value = rdr["Quantity"].ToString(); ;
orderHistoryProductDataGrid.Rows.Add(row);
count++;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}`

Data successfully retrieving from database. But rows are not added to my datagridview. Pls help...

Upvotes: 2

Views: 611

Answers (1)

mybirthname
mybirthname

Reputation: 18137

OrderHistory OH = new OrderHistory();
public void viewOrderHistory(int id)
{

    DataGridView orderHistoryProductDataGrid = OH.dataGridView2;
    try
    {
        DatabaseConnection();//Database connection
        string ord = "SELECT * FROM Orders_Products WHERE ID=@ID";
        SqlCommand ordPro = new SqlCommand(ord, myCon);
        ordPro.Parameters.AddWithValue("@ID", id);

        DataSet resultDst = new DataSet();
        using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
        {
            adapter.Fill(resultDst, "OrderProducts");
        }

        orderHistoryProductDataGrid.DataSource = resultDst.Tables[0]; 
     }
     catch (Exception ex)
     {
        MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
}`

First, you should not write queries kike this ID='" + id + "'" with this you are open to sql injection, you can read about that in wikipedia. Also this impact the sql server performance. Always use command parameters in your queries.

Second, you can use SqlDataAdapter to fetch the data from Database in DataSet and after that put it as DataSource to the Grid ! You can read about SqlDataAdapter.

Third, be aware that your formatting was really pure and because of that probably nobody answer you ! Try to do it better in the future. Also don't write your tables in the data base with '_'. Instead Orders_Products just write OrderProducts

Also better practice is to have your connection to the database in different layer. I wrote a data access layer in other question: checking user name or user email already exists ! In this question I explain in detail how you can do it, if you want check it. This will improve your knowledge.

Upvotes: 1

Related Questions