Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone.....
I want to add rows dynamically in datagrid in WPF
Is it right ?
C#
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
Updated 17-Sep-12 21:20pm
v2
Comments
[no name] 18-Sep-12 9:46am    
You might want to show the code where you are actually adding values to the row

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.
 
Share this answer
 
Comments
krushna chandra jena 19-Sep-12 6:05am    
here i can not apply data binding approach. Please provide another solution ....
Adam David Hill 19-Sep-12 6:06am    
Could you explain why data-binding won't work? Perhaps show some surrounding code?
krushna chandra jena 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..
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:
C#
FileInfo[] files = new DirectoryInfo(System.IO.Path.GetTempPath()).GetFiles();
foreach (FileInfo fi in files)
{
    if (fi != null)
    {
        dataGrid1.Items.Add(fi);
    }
}


The XAML bit:
XML
<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.
 
Share this answer
 
v2
Comments
krushna chandra jena 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 ?
Adam David Hill 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
Top Experts
Last 24hrsThis month


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