在按钮单击时手动将文本框中的文本添加到数据网格 [英] manually adding text from textbox to a datagrid on button click

查看:109
本文介绍了在按钮单击时手动将文本框中的文本添加到数据网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了名为学生姓名的静态列& Village 使用 XAML ,如下所示:

I created static columns called "Student Name" & "Village" using XAML as below:

<my:DataGrid.Columns >
      <my:DataGridTextColumn Header="Student Name" Width="200">
      </my:DataGridTextColumn>
      <my:DataGridTextColumn Header="Village" Width="200">
      </my:DataGridTextColumn>
</my:DataGrid.Columns>





尝试使用按钮下的代码从文本框添加文本:



Trying to add text from textbox using code under a button:

Dim row1() As String = {TextBox2.Text.ToString, TextBox3.Text.ToString}
DataGrid1.Items.Add(row1)





已添加行但没有tex t。

可能是什么问题?



格式化的代码块[/ Edit]



Rows are added but with no text.
What could be the problem?

Code blocks formatted[/Edit]

推荐答案

试试这个



创建一个结构



Try this

Create a structure

Public Structure MyData
       Public Property id() As Integer
           Get
               Return m_id
           End Get
           Set(value As Integer)
               m_id = value
           End Set
       End Property
       Private m_id As Integer
       Public Property title() As String
           Get
               Return m_title
           End Get
           Set(value As String)
               m_title = value
           End Set
       End Property
       Private m_title As String
   End Structure





然后在你的按钮点击事件中使用以下





then in your button click event use the following

Dim col1 As New DataGridTextColumn()
  Dim col2 As New DataGridTextColumn()

  DataGrid1.Columns.Add(col1)
  DataGrid1.Columns.Add(col2)

  col1.Binding = New Binding("id")
  col2.Binding = New Binding("title")

  col1.Header = "ID"
  col2.Header = "title"


  DataGrid1.Items.Add(New MyData() With { _
    .id = 1, _
    .title = "Test"
  })







它代码粗略但可以帮助你前进




its a rough bit of code but should help you move forward


你好braop,我有一些想法。



第一:你可以使用手动创建的DataTable,如@petermahon所解释的那样。



第二:如果要直接在控件中添加自定义文本,则不能使用 DataGridTextColumn ,因为此属性需要一些DataBinding。您可以在其中使用带有TextBlock的 DataGridTemplateColumn ,然后您可以添加XAML或C#中的文本。



告诉我。< br $> b $ b

祝你好运。
Hi braop, I have some ideas.

First: You can use a DataTable created manually as @petermahon explained.

Second: If you want to add custom text directly in your control you cannot use DataGridTextColumn because this Property expects some DataBinding. You can use a DataGridTemplateColumn with a TextBlock inside it and then you can add text from your XAML or C#.

Let me know.

Best regards.


这篇关于在按钮单击时手动将文本框中的文本添加到数据网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆