Quantcast
Viewing all articles
Browse latest Browse all 18858

create datagrid fist column to collection of list in WPF

basically I am doing is to collect the files from local drives and copy them all to C:\ReportFiles\. I have created two buttons. Attach and Save. Attach will browse and get the files. In dataGrid1, I am taking the old path and filename into datagrid column1(path) and column2(filename). When finish collecting all the files. Click on save button. This will take Path column from datagrid and start coping file from datagrid Path column to C:\ReportFiles\. The loop will do this job. 

The problem is the list, I cant make datagrid column as list of collection. I know how to get the list to datagrid but here I have to collect the column from datagrid to list<>.  please help.Thanks below is all the code. 

DataGrid code:

<DataGrid AutoGenerateColumns="False" Height="193" HorizontalAlignment="Left" Margin="169,6,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="200" ItemsSource="{Binding ReportFiles}">

               <DataGrid.Columns>

                   <DataGridTextColumn Header="Path" Binding="{Binding Path}" />

                    <DataGridTextColumn Header="FileName" Binding="{Binding FileName}" />

               </DataGrid.Columns>

           </DataGrid>

MainWindow.cs:

 

publicpartialclass EditReport : Window

{

       publicList<ReportFiles> listabove =newList<ReportFiles>();

 

publicclassReportFiles

    {

       publicstring RealName {get; set; }

       publicstring TargetName {get; set; }

    }

      privatevoid buttonAttach_Click(object sender,RoutedEventArgs e)

        {

              Microsoft.Win32.OpenFileDialog dlg =new Microsoft.Win32.OpenFileDialog();

              if (dlg.ShowDialog() ==true)

              {

foreach (string strin dlg.FileNames)

                     {

                     ReportFiles list = newReportFiles();

                     list.RealName = str;

                     list.TargetName = filename;

                     dataGrid1.Items.Add(new { RealName = list.RealName, TargetName = list.TargetName });

                    string fileName = @"C:\Temp\"+ filename + System.IO.Path.GetExtension(str).Trim(); ;

                   if (File.Exists(fileName))

                   continue;

                   else

                        {

                       try

                        {

                            File.Copy(str, fileName);

                        }

                       catch (Exception err)

                             {

                            MessageBox.Show(err.Message);

                            return;

                             }

                        }

                     }

              }

}

privatevoid btnSave_Click(object sender,RoutedEventArgs e)

       {

           ReportFiles rep = newReportFiles();

           DataRowView paths = (System.Data.DataRowView)dataGrid1.Items[0];

            rep.RealName =Convert.ToString(paths.Row.ItemArray[0]);

           

       }

}



Viewing all articles
Browse latest Browse all 18858

Trending Articles