-
Sep 6th, 2012, 09:36 PM
#1
Thread Starter
Member
[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]
-
Sep 7th, 2012, 12:46 AM
#2
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.
-
Sep 7th, 2012, 08:24 PM
#3
Thread Starter
Member
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.
-
Sep 9th, 2012, 10:18 PM
#4
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!
-
Sep 10th, 2012, 08:53 PM
#5
Thread Starter
Member
Re: Displaying item on datagrid?
Yea that did the trick. Thanks! Everything is resolved now.
-
Sep 10th, 2012, 09:22 PM
#6
Re: Displaying item on datagrid?
Great!
Please mark this thread as resolved...
Cheers!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|