C# 哪种方法是将选定值添加到数据网格中的快速好方法
例如,在使用以下控件的窗体中C# 哪种方法是将选定值添加到数据网格中的快速好方法,c#,vb.net,datagrid,C#,Vb.net,Datagrid,例如,在使用以下控件的窗体中 组合框(cboProducts) 文本框(txtQty) trueDBGrid(使用组件一控件)(grdSale) 所以, cboProducts正在填写产品表中的产品名称、产品Id、, 一旦选择了一个产品(cboProducts),下一步就是输入数量(txtQty) 输入数量后,应使用上述输入的项目填充网格 我的计划 将输入的产品、数量添加到Datatbale中,然后将Datatable添加到mygrdSale 我认为会有另一个好的方法可用,我希望有好的答案
- 组合框(
)cboProducts
- 文本框(
)txtQty
- trueDBGrid(使用组件一控件)(
)grdSale
cboProducts
正在填写产品表中的产品名称、产品Id、,
一旦选择了一个产品(cboProducts
),下一步就是输入数量(txtQty
)
输入数量后,应使用上述输入的项目填充网格
我的计划
将输入的产品、数量添加到Datatbale
中,然后将Datatable
添加到mygrdSale
我认为会有另一个好的方法可用,我希望有好的答案
谢谢我倾向于使用这样的东西, 这假定列已定义
grdSale.Rows.Clear()
For Each item As myObject In MyObjects
Dim row As New DataGridViewRow
row.CreateCells(grdSale)
row.Cells(0).Value = item.PKId
row.Cells(1).Value = item.Product_Desc
row.Cells(2).Value = item.Quantity
row.Cells(3).Value = item.Price
item.Rows.Add(row)
Next
GridView使用数据源。在您的例子中,它使用数据表作为源。 (我是一名c#开发人员,所以语法可能有点不对劲) 当您成功地为控件创建事件时,只需将新行添加到datatable中,更改将在网格中可见
Sub AddRow(byval productID as Integer, byval productName as string, byval qty as Integer)
dim row as System.Data.DataRow
row = dt.NewRow() 'this gets a fresh row from the datatable. Since you already have the datatable defined, the for will have the appropriate columns for your data
row("productID") = productID
row("productName") = productName
row("qry") = qry
dt.Rows.Add(row) ' at this line you add the new row to the datatable which is "viewed" by a grid and which in turn shows the new information inside itself.
End Sub
这个怎么样
Private Sub cboProducts_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cboProducts.KeyDown
If e.KeyCode = Keys.Enter Then
Dim pdt, pdtid As String
Dim i As Integer
pdt = cboProducts.Columns(0).Text
pdtid = cboProducts.Columns(1).Text.ToString
grdSale.SetDataBinding()
If grdSale.Rows.Count = 0 Then
i = 0
Else
i = i + grdSale.Rows.Count
End If
grdSale.AddRows(1)
grdSale(i, 0) = pdt
grdSale(i, 1) = pdtid
End If
End Sub
这是一个很好的尝试,但您必须只调用一次
grdSale.SetDataBinding()
,这意味着更好地使用它Load Event
Private Sub cboProducts_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cboProducts.KeyDown
If e.KeyCode = Keys.Enter Then
Dim pdt, pdtid As String
Dim i As Integer
pdt = cboProducts.Columns(0).Text
pdtid = cboProducts.Columns(1).Text.ToString
grdSale.SetDataBinding()
If grdSale.Rows.Count = 0 Then
i = 0
Else
i = i + grdSale.Rows.Count
End If
grdSale.AddRows(1)
grdSale(i, 0) = pdt
grdSale(i, 1) = pdtid
End If
End Sub