2016-09-21 59 views
-1

你好,大家好我試圖在這樣的winform DataGrid中......如何更新行值DataGrid中動態地在winform

enter image description here

所以到現在,我能夠顯示數據網格數據,如這

enter image description here

IAM正從數據庫中的所有值... ,所以我想那是什麼,如果我點擊+按鈕,然後這是硬編碼值必須增加一個數量列的值d總量也必須進行更新,但是這應該是隻有前端....

這裏是我的代碼..

公共無效getsubitemdetails(){

 string selectSql = "select ItemGroupsAssociations.Price, Items.ItemName from ItemGroupsAssociations INNER JOIN Items ON ItemGroupsAssociations.ItemId = Items.ItemId where ItemGroupsAssociations.CategoryId=1 AND ItemGroupsAssociations.ItemId=1"; 
     //string selectSql = "select * from Categories"; 
     SqlCommand cmd = new SqlCommand(selectSql, con); 

     try 
     { 
      con.Open(); 
      using (SqlDataReader reader = cmd.ExecuteReader()) 
      { 
       while (reader.Read()) 
       { 
        // int id = Convert.ToInt32(reader["CategoryId"]); 
        string Categoryname = (reader["ItemName"].ToString()); 
        Price = Convert.ToInt32(reader["Price"]); 

        int quantity = 2; 
        int totalamount = quantity * Price; 

        dataGridView1.ColumnCount = 1; 
        dataGridView1.Columns[0].Name = "Name"; 
        dataGridView1.Columns[0].Width = 300; 
        dataGridView1.Rows.Add(Categoryname); 

        DataGridViewButtonColumn btn = new DataGridViewButtonColumn(); 
        dataGridView1.Columns.Add(btn); 
        btn.HeaderText = "Quantity"; 
        btn.Text = quantity.ToString(); 
        btn.Width = 50; 
        btn.Name = "btn1"; 
        btn.UseColumnTextForButtonValue = true; 

        DataGridViewButtonColumn btn1 = new DataGridViewButtonColumn(); 
        dataGridView1.Columns.Add(btn1); 
        btn1.HeaderText = "Rate"; 
        btn1.Text = Price.ToString(); 
        btn1.Width = 50; 
        btn1.Name = "btn2"; 
        btn1.UseColumnTextForButtonValue = true; 

        DataGridViewButtonColumn btn2 = new DataGridViewButtonColumn(); 
        dataGridView1.Columns.Add(btn2); 
        btn2.HeaderText = "Total Amount"; 
        btn2.Text = totalamount.ToString(); 
        btn2.Width = 100; 
        btn2.Name = "btn2"; 
        btn2.UseColumnTextForButtonValue = true; 

        DataGridViewButtonColumn btn3 = new DataGridViewButtonColumn(); 
        dataGridView1.Columns.Add(btn3); 
        btn3.HeaderText = ""; 
        btn3.Text = "+"; 
        btn3.Width = 50; 
        btn3.Name = "btn3"; 
        btn3.UseColumnTextForButtonValue = true; 
       } 
      } 
     } 
     finally 
     { 
      con.Close(); 
     } 
    } 

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) 
     { 
      if (e.ColumnIndex == 4) 
      { 
       Price= Price+1; 

       MessageBox.Show((e.RowIndex + 1) + " Row " + (e.ColumnIndex + 1) + " Column button clicked "); 
      } 
     } 

我怎樣才能實現這一點...請幫助我,這將是偉大的幫助或任何其他建議,以便我可以使用...

謝謝你們。

回答

0

可能的答案:

填充一個DataTable使用數據的SQL命令 注意的DataColumn數量與硬編碼值1,只要你喜歡

SELECT 
    ItemGroupsAssociations.Price, 
    Items.ItemName, 1 Quantity 
FROM ItemGroupsAssociations 
INNER JOIN Items 
    ON ItemGroupsAssociations.ItemId = Items.ItemId 
WHERE ItemGroupsAssociations.CategoryId = 1 
    AND ItemGroupsAssociations.ItemId = 1* 

DataTable dt = new DataTable("the name you like"); 

dt.Load(reader) 

添加一個額外的創建數據列名Total

dt.Columns.Add(new DataColumn("Total", typeof(decimal))) 
dt.Columns["Total"].Expression = "Quantity * Price" 

將DataTable綁定到DataGridView