2

In a WPF application, by using c#, I want users to be able to import their data to grid view. So do i need a browse button or something?

If yes how can I do that?

theduck
  • 2,526
  • 13
  • 15
  • 22
hslldm
  • 43
  • 1
  • 1
  • 4
  • check this answer : http://stackoverflow.com/a/10315283/161222 – Radi Feb 07 '13 at 16:50
  • Ohhhhmmmm...my glass ball says it's broken. From where shall the user be able to import the data? From file, clipboard... What do you mean by _browse button_? – DHN Feb 07 '13 at 16:51
  • what type of file are you trying to import? You'll need to parse through the data in order to fill your datagrid, there is no magic import functionality in a datagrid, you need to implement it yourself. The browse button is the easy part. – Lee Harrison Feb 07 '13 at 17:13
  • Add a button to the window in an appropriate place: ``. Honestly, you should at least include what you are trying to do, where you have problems and what you tried so far. – Joey Feb 07 '13 at 17:17

1 Answers1

15

The below code helps you to display the browse button

<TextBox Height="32" HorizontalAlignment="Left" Margin="6,10,0,0" Name="FileNameTextBox"
                 VerticalAlignment="Top" Width="393" />
        <Button Content="Browse" Height="32" HorizontalAlignment="Left" Margin="405,10,0,0"
                Name="button1" VerticalAlignment="Top" Width="88" Click="button1_Click" />


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

// Set filter for file extension and default file extension
dlg.DefaultExt = ".txt";
dlg.Filter = "Text documents (.txt)|*.txt";

// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();

// Get the selected file name and display in a TextBox
if (result == true)
{
    // Open document
    string filename = dlg.FileName;
    FileNameTextBox.Text = filename;
 }
Smaug
  • 2,505
  • 4
  • 26
  • 42