2016-08-24 153 views
-1

我已經試過這一點,但沒有得到選中的值給另一個網格視圖的DataGridView使用複選框

string data = ""; 
    foreach (GridViewRow row in GridView5.Rows) 
    { 
     if (row.RowType == DataControlRowType.DataRow) 
     { 
      CheckBox chkRow = (row.Cells[0].FindControl("chkCtr") CheckBox); 
      if (chkRow.Checked) 
      { 
       string Brand_Name = row.Cells[1].Text; 
       string Market_Name = row.Cells[2].Text; 
       string USC = row.Cells[3].Text; 
       string Manufacture = row.Cells[4].Text; 
       data = data + Brand_Name + " , "; 
       //;+ Market_Name + " , " + USC + ","+Manufacture+""; 

       gvrProducts.Visible = true; 


       gvrProducts.DataSource = data; 
       // ListBox1.DataBind(); 
       gvrProducts.DataBind(); 
      } 
     } 
    } 

請幫

回答

0

嘗試添加該

Private Sub Gridview_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles Gridview.CurrentCellDirtyStateChanged 
    Gridview.CommitEdit(DataGridViewDataErrorContexts.Commit) 
End Sub 

然後

Private Sub Gridview_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles Gridview.CellValueChanged 
    Try 
     Gridview.CommitEdit(DataGridViewDataErrorContexts.Commit) 
     'add commit when chckbox is checked. Then your procedure/staff here. 
     'Or call your for loop here 
    Catch ex As Exception 
    End Try 
End Sub 
1

GridView需要data source control,如LinqDataSource,ObjectDataSource或SqlDataSource控件。你正在綁定你的GridView和一個字符串變量(不知道它如何綁定數據)。有很多方法可以綁定gridview,但我通過創建動態數據表的方式來解釋:

創建一個空DataTable for第二GridView控件:

DataTable dt = new DataTable(); 
DataRow dr = null; 
dt.Columns.Add(new DataColumn("Brand_Name", typeof(string))); 
dt.Columns.Add(new DataColumn("Market_Name", typeof(string))); 
dt.Columns.Add(new DataColumn("USC", typeof(string))); 
dt.Columns.Add(new DataColumn("Manufacture", typeof(string))); 

的行添加到該數據表中您的foreach循環內部之後:

foreach (GridViewRow row in GridView5.Rows) 
     { 
      if (row.RowType == DataControlRowType.DataRow) 
      { 
       CheckBox chkRow = (row.Cells[0].FindControl("chkCtr") CheckBox); 
       if (chkRow.Checked) 
       { 
       dr = dt.NewRow(); 
       dr["Brand_Name"] = row.Cells[1].Text; 
       dr["Market_Name"] = row.Cells[2].Text; 
       dr["USC"] = row.Cells[3].Text; 
       dr["Manufacture"] = row.Cells[4].Text; 
       dt.Rows.Add(dr);     

       } 
      } 
     } 

然後綁定的DataTable

你的GridView
+0

哇,這是最終的解決方案!! ..謝謝。我會帶來更多疑問;) – Prashanth