Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET WPF
Hi Everyone.....
I want to add rows dynamically in datagrid in WPF
Is it right ?
 dataGrid1.Items.Add(object newitem);
because in my program actually rows added but values are not shown ...means blank rows..only number of lines are incremented.
 

Thank You
Posted 17-Sep-12 22:18pm
Edited 17-Sep-12 22:20pm
v2
Comments
Wes Aday at 18-Sep-12 9:46am
   
You might want to show the code where you are actually adding values to the row
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Rather than adding new objects to the ".items" collection you may be better off taking a databinding approach. By sounds of things you are in fact adding the row successfully, but haven't told the datagrid how it should display the data in that row (using a data template, for example). Check out this tutorial for more info: http://wpftutorial.net/DataGrid.html
 
If this is not the case then we'll need to see some code, preferably both XAML & C#.
 
Hope that helps.
  Permalink  
Comments
krushna chandra jena at 19-Sep-12 6:05am
   
here i can not apply data binding approach. Please provide another solution ....
AdamDavidHill at 19-Sep-12 6:06am
   
Could you explain why data-binding won't work? Perhaps show some surrounding code?
krushna chandra jena at 19-Sep-12 7:41am
   
actually i am new in WPF ,can not implement such type of codes.
i want to show all temporary files in one datagrid .
 
my code is
 
FileInfo[] files = new DirectoryInfo(System.IO.Path.GetTempPath()).GetFiles();
foreach (FileInfo fi in files)
{
if (fi != null)
{
//here i want to add files(as rows ) in datagrid
}
}
 
which code is needed to bind rows in datagrid dynamically ? please help..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Okay, here's a fully working example. Note that I'm expressing which columns I want to display, and how, in the XAML.
 
The C# bit:
FileInfo[] files = new DirectoryInfo(System.IO.Path.GetTempPath()).GetFiles();
foreach (FileInfo fi in files)
{
    if (fi != null)
    {
        dataGrid1.Items.Add(fi);
    }
}
 
The XAML bit:
<DataGrid x:Name="dataGrid1">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Name">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="DirectoryName">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding DirectoryName}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>
 
Please mark as the answer if that helps.
  Permalink  
v2
Comments
krushna chandra jena at 21-Sep-12 0:55am
   
Thank You....it works....
thank You once again.......
one more question is how could you divide two columns and give them to their particular tasks without written any back end code means in datagrid , two columns (one is File Name and another is Directory Name) contain values of File and Directory_Path respectively but where you assign that Name_Column could contains only name of file and Directory_Column could contains only Directory_path ?
Please Explain ?
AdamDavidHill at 21-Sep-12 4:01am
   
It would be easier to do it with backend code, and when you loop through the files creating a list, actually make a list of a custom class instead, where you would programmatically manipulate the strings. You can use StringFormat to modify strings in bindings but I'm not sure it's powerful to do what you ask. Another approach is to write a custom IValueConverter (just search for a tutorial) on that column's binding. This converter would take the full path and trim it down to just what you need.
 
Glad it worked for you.

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

  Print Answers RSS
0 CHill60 365
1 Sergey Alexandrovich Kryukov 245
2 Peter Leow 180
3 CPallini 150
4 Zoltán Zörgő 150
0 Sergey Alexandrovich Kryukov 9,208
1 OriginalGriff 6,796
2 Peter Leow 4,417
3 Zoltán Zörgő 3,849
4 CHill60 2,702


Advertise | Privacy | Mobile
Web03 | 2.8.150128.1 | Last Updated 19 Sep 2012
Copyright © CodeProject, 1999-2015
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