Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I created static columns called "Student Name" & "Village" using XAML as below:
XML
<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:
VB
Dim row1() As String = {TextBox2.Text.ToString, TextBox3.Text.ToString}
DataGrid1.Items.Add(row1)


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

[Edit]Code blocks formatted[/Edit]
Posted
v3
Comments
Sergey Alexandrovich Kryukov 10-Feb-13 13:47pm    
What does "manual" mean? What did you try, anyway?
—SA
braop 11-Feb-13 1:24am    
directly adding text from textbox to datagrid...i used that word cause i ain't picking data from the database...
Aydin Homay 7-Apr-13 0:59am    
Create Data row object and use it ;)

Try this

Create a structure

VB
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

VB
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
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900