TextBox绑定到DataTable [英] TextBox bound to DataTable
问题描述
大家好
我有一个包含多个文本框的Win Form
I have a Win Form with several textboxs
每个文本框都绑定到DataTable Column(运行时),带有代码喜欢:
Each text box bounds to DataTable Column (runtime) with code like :
' get Column Name
col = UIS_DT_Clienti.ClienteDataCreazioneColumn.ColumnName
'create Binding object
BindingClienteDataCreazione = New Binding("Text", UIS_DT_Clienti, col)
' set bound
TxtClienteDataCreazione.DataBindings.Add(BindingClienteDataCreazione)
使用按钮我浏览数据表并选择当前行,使所有文本框填充数据列值
With buttons I navigate thru DataTable and choose a current row getting all textboxs filled with data column value
现在, 在表单中我有一个按钮在DataTable中插入新行。
Now, in the form I have a button to insert New Row in DataTable.
问题是 在这种情况下,所有TextBox都应为空,但它们仍然填充了最后一行。
The problem is that in this case all TextBox should be Empty, but they are still filled with last current row.
那么当 时,如何获取具有空值的文本框我需要插入新行吗?
So How to get Textboxs with Empty values when I need to insert new row ?
推荐答案
它有当前行的原因是你还没有移动到新行。在下面我多年前的例子中,并没有创建它显示这个。
The reason why it has the current row is you have not move to the new row. In the example below which I had from many years ago, did not just create it show this.
如果我按下"添加"按钮。在BindingNavigator中创建一个新行并移动到它,按下Button2也是如此。按下Button1,我们就像你现在一样保持当前行。
If I press the "Add" button in the BindingNavigator a new row is created and moves to it, press Button2 does the same. Press Button1 and we remain on the current row as you have now.
Public Class Form1
Private _bs As New BindingSource
Private Sub b_Format(sender As Object, cevent As ConvertEventArgs)
If cevent.DesiredType IsNot GetType(String) Then
Exit Sub
End If
cevent.Value = CType(cevent.Value, Decimal).ToString("c")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As New DataTable
dt.Columns.Add(New DataColumn With {.ColumnName = "C1", .DataType = GetType(Decimal)})
dt.Rows.Add(New Object() {199999})
dt.Rows.Add(New Object() {20})
dt.Rows.Add(New Object() {10})
_bs.DataSource = dt
BindingNavigator1.BindingSource = _bs
Dim b As Binding = New Binding("Text", _bs, "C1")
AddHandler b.Format,
Sub(s As Object, args As ConvertEventArgs)
If Not String.IsNullOrWhiteSpace(args.Value.ToString) Then
If Not IsDBNull(args.Value) Then
args.Value = Convert.ToInt32(args.Value).ToString("C2")
Else
args.Value = "N/A" ' whatever makes sense
End If
Else
args.Value = "N/A" ' whatever makes sense
End If
End Sub
AddHandler b.Parse,
Sub(s As Object, args As ConvertEventArgs)
If args.DesiredType IsNot GetType(Decimal) Then
Exit Sub
End If
If Not String.IsNullOrWhiteSpace(args.Value.ToString) Then
args.Value = Decimal.Parse(args.Value.ToString, Globalization.NumberStyles.Currency, Nothing)
Else
args.Value = 0
End If
End Sub
UnitPriceTextBox.DataBindings.Add(b)
End Sub
''' <summary>
''' If you simply add the row and not move to it the current row
''' remains current while using MoveLast we are now on the new row
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
CType(_bs.DataSource, DataTable).Rows.Add()
_bs.MoveLast()
End Sub
''' <summary>
''' Add new row and move to the new row
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
_bs.AddNew()
End Sub
End Class
此处出现N / A因为格式事件逻辑
Here N/A appears because of the Format event logic
这篇关于TextBox绑定到DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!