Results 1 to 6 of 6

Thread: [Resolved]Displaying item on datagrid?

  1. #1

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    52

    [Resolved]Displaying item on datagrid?

    I've been trying to add an item to my datagrid but when I add one it just displays a blank row. When I use msgbox to see the item text it shows the value I want to display. So my question is, why doesn't the datagrid display "test"?

    Here's my vb:
    Code:
    Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
    	'add columns to datagrid
    	Dim linkColumn As New DataGridTextColumn : Dim titleColumn As New DataGridTextColumn
    	linkColumn.Header = "Links:" : titleColumn.Header = "Titles:"
    	linkColumn.Width = DataGrid1.Width / 2 : titleColumn.Width = DataGrid1.Width / 2
    	linkColumn.Binding = New System.Windows.Data.Binding("Links") : titleColumn.Binding = New System.Windows.Data.Binding("Titles")
    	DataGrid1.Columns.Add(linkColumn) : DataGrid1.Columns.Add(titleColumn)
    
    	mainTimer.Interval = 5000 : mainTimer.Enabled = True
    
    	DataGrid1.Items.Add("test")
    	MsgBox(DataGrid1.Items.Item(0).ToString) 'although the datagrid won't show it, this displays "test". why?
    End Sub
    Not sure if it's needed to help me but here's my XAML:
    Code:
    <DataGrid AutoGenerateColumns="False" Height="289" HorizontalAlignment="Left" Margin="10,10,0,0" Name="DataGrid1" VerticalAlignment="Top" Width="481" Grid.ColumnSpan="2" ItemsSource="{Binding}" />
    Last edited by code; Sep 11th, 2012 at 12:18 PM. Reason: [Resolved]

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: Displaying item on datagrid?

    Hi,

    Here's a code sample:

    Code:
    private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                DataGridTextColumn c1 = new DataGridTextColumn();
                c1.Header = "Test";
                c1.Binding = new Binding("test");
                c1.Width = 473;
                dataGrid.Columns.Add(c1);
                dataGrid.Items.Add(new Names() {test="just testing"});
         
            }
    And here's a simple class declaration

    Code:
    class Names
        {
            public string test { get; set; }
        }
    Make sure the column is bound to the property of Names class.
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    52

    Re: Displaying item on datagrid?

    Ok so I tried something like that:
    Code:
    DataGrid1.Items.Add(New DataItems() With {.Link = "http://test.com", .Title = "test"})
    and the class for dataitems is:
    Code:
    Public Class DataItems
        Private _link As String
        Public Property Link() As String
            Get
                Return _link
            End Get
            Set(l As String)
                _link = l
            End Set
        End Property
    
        Private _title As Integer
        Public Property Title() As Integer
            Get
                Return _title
            End Get
            Set(t As Integer)
                _title = t
            End Set
        End Property
    End Class
    But it didn't display anything on runtime.

  4. #4
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: Displaying item on datagrid?

    Hi,

    Your property title is wrong, you should change the property type to String. It will create a type cast error.

    I've created a VB version out from this.

    Here are some of the snippets:

    Code:
     Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
    
            Dim linkColumn As New DataGridTextColumn : Dim titleColumn As New DataGridTextColumn
            linkColumn.Header = "Links:" : titleColumn.Header = "Titles:"
            linkColumn.Width = dataGrid.Width / 2 : titleColumn.Width = dataGrid.Width / 2
            linkColumn.Binding = New System.Windows.Data.Binding("Link") : titleColumn.Binding = New System.Windows.Data.Binding("Title")
            dataGrid.Columns.Add(linkColumn) : dataGrid.Columns.Add(titleColumn)
            dataGrid.Items.Add(New DataItems() With {.Link = "http://testingme.com", .Title = "mytesting"})
        End Sub
    Code:
    Public Class DataItems
        Private _link As String
        Public Property Link() As String
            Get
                Return _link
            End Get
            Set(ByVal l As String)
                _link = l
            End Set
        End Property
    
        Private _title As String
        Public Property Title() As String
            Get
                Return _title
            End Get
            Set(ByVal t As String)
                _title = t
            End Set
        End Property
    End Class
    For the xaml code:

    Code:
    <Grid>
            <DataGrid AutoGenerateColumns="False" Height="289" HorizontalAlignment="Left" Margin="10,10,0,0" Name="dataGrid" 
                      VerticalAlignment="Top" Width="481" Grid.ColumnSpan="2" ItemsSource="{Binding }">
            </DataGrid>
        </Grid>

    This works fine on my end..

    Cheers!
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  5. #5

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    52

    Re: Displaying item on datagrid?

    Yea that did the trick. Thanks! Everything is resolved now.

  6. #6
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: Displaying item on datagrid?

    Great!

    Please mark this thread as resolved...

    Cheers!
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width