Click here to Skip to main content
12,943,367 members (79,662 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
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)


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

[Edit]Code blocks formatted[/Edit]
Posted 10-Feb-13 3:29am
braop554
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 EBRAHIMI HOMAY 7-Apr-13 0:59am
   
Create Data row object and use it ;)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,643
CHill60 2,930
Maciej Los 2,328
Jochen Arndt 1,900
ppolymorphe 1,765


Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 13 Mar 2014
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100