Friday, September 7, 2012

Save datagrid content to a text file.

Save datagrid content to a text file.

Hi Experts ,

I am facing problem to save the content of datagrid to a text file .

Below is my code.Can you please check this.

//DataGrid name-dgSelectedProvider

SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "All Files (*.*)|*.*|Text Files (*.txt)|*.txt";

if (sfd.ShowDialog().Value)
{
using (StreamWriter sw = new StreamWriter(sfd.OpenFile()))
{
sw.Write(dgSelectedProvider);
sw.AutoFlush = true;
sw.Flush();
}
}

Answers & Comments...

Answer: 1

http://www.codeproject.com/Articles/45731/Export-Silverlight-DataGrid-to-Excel-XML-CSV

http://www.dotnetcurry.com/ShowArticle.aspx?ID=651

http://stackoverflow.com/questions/304322/silverlight-datagrid-export-to-excel-or-csv



Answer: 2

Hi BuntyBubly,

BuntyBubly

sw.Write(dgSelectedProvider);

If you want to save the content of the datagrid to a text file, you need get the contents of each field( reture as a string) and then write those contents to a text file. If your datagrid defined similar to below codes, you can get the TextBlock from datagrid's LoadingRow event, then get the content of the TextBlock.

<sdk:DataGrid x:Name="datagrid1" LoadingRow="datagrid1_LoadingRow" ItemsSource="{Binding Datas,Source={StaticResource MainViewModel},Mode=TwoWay}" AutoGenerateColumns="False" Canvas.Left="0" Canvas.Top="40">                  <sdk:DataGrid.Columns>                      <sdk:DataGridTemplateColumn Header="Name" Width="110">                          <sdk:DataGridTemplateColumn.CellTemplate>                              <DataTemplate>                                  <TextBlock Text="{Binding Name}" />                              </DataTemplate>                          </sdk:DataGridTemplateColumn.CellTemplate>                       ...                    </sdk:DataGrid.Columns>              </sdk:DataGrid>  
private void datagrid1_LoadingRow(object sender, DataGridRowEventArgs e)          {                DataGridRow row = e.Row;              DataGridColumn column = datagrid1.Columns[0];              FrameworkElement fe = column.GetCellContent(row);              TextBlock hltb = fe as TextBlock;             ...            }

For more information on using SaveFileDialog, you can refer:

http://www.silverlightshow.net/items/Using-the-SaveFileDialog-in-Silverlight-3.aspx

Hope it helps.

Best Regards,



Answer: 3

Have a look at http://www.qa.com/about-qa/blogs/2012/august/copy-all-to-clipboard-a-silverlight-datagrid-story-in-three-iterations/





1 comment:

KITS Technologies said...

Nice article | It was very useful for me. I have founded more details about Salesforce Online Training
structs online online training
Webmethods online online training
Wise package studio online online training

Post a Comment

Send us your comment related to the topic mentioned on the blog