Showing posts with label WPF. Show all posts
Showing posts with label WPF. Show all posts

Friday, February 25, 2011

Binding Data to Data Grid in WPF


In this post we are going to learn how to bind data to DataGrid in WPF(Windows Presentation Foundation)  
Create new project named as DataGridSample 
  



To show a basic data grid, just drop a DataGrid control to your view and bind the ItemsSource to a data table
The DataGrid provides a feature called AutoGenerateColumns that automatically generates column according to the columns of your data datatable. It generates the following types of columns:
  • TextBox columns for string values
  • CheckBox columns for boolean values
  • ComboBox columns for enumerable values
  • Hyperlink columns for Uri values
MainWindow.xaml

<Grid>
      <DataGrid AutoGenerateColumns="True" Height="225" HorizontalAlignment="Left" Margin="28,21,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="408" />
</Grid>

MainWindow.xaml.cs

        SqlConnection cn = new SqlConnection("Data     Source=.;database=Practice;trusted_connection=true");
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            cn.Open();
            SqlDataAdapter da = new SqlDataAdapter("Select * from Employee", cn);
            DataSet ds = new DataSet();
            da.Fill(ds, "Employee");
            dataGrid1.ItemsSource = ds.Tables["Employee"].DefaultView;
        }
Result Window


Alternatively you can define your columns manually by setting the AutoGenerateColumns property to False.

Here we can add following type of columns

DataGridCheckBoxColumn
DataGridComboBoxColumn
DataGridHyperlinkColumn               
DataGridTemplateColumn
DataGridTextColumn

MainWindow.xaml

<DataGrid AutoGenerateColumns="False" Height="173" HorizontalAlignment="Left" Margin="12,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="292" ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding EmpId}" Header="Id" />
<DataGridTextColumn Binding="{Binding EmpName}" Header="Name" />
<DataGridTextColumn Binding="{Binding Designation}"    Header="Designation" />
<DataGridTextColumn Binding="{Binding Location}" Header="Location" />
<DataGridCheckBoxColumn Binding="{Binding IsActive}" Header="IsActive" />
 </DataGrid.Columns>
 </DataGrid>


Result Window

 

How to use OpenFileDialog in WPF


The OpenFileDialog is used to browse files on a machine. 

The OpenFileDialog class defined in Microsoft.Win32.OpenFileDialog namespace represents an OpenFileDialog control in WPF. 

Let's add a TextBox and a Button control to a XAML page. The window looks like this.








When you click the Browse button, we will browse text files and set the TextBox.Text to the selected file name.
 
<TextBox Height="25" HorizontalAlignment="Left" Margin="41,104,0,0" Name="txtFileUpload" VerticalAlignment="Top" Width="405" Grid.RowSpan="2" />

<Button Content="Browse" Height="26" HorizontalAlignment="Left" Margin="448,103,0,0" Name="btnBrowse" VerticalAlignment="Top" Width="75" Click="btnBrowse_Click" />
 
The code listed in Listing 1 creates an OpenFileDialog, set its default extension and filter properties and calls ShowDialog method that displays the dialog. Once the OpenFileDialog is displayed, you can use it to browse files. Once file is selected, the FileName property of the OpenFileDialog is set to the selected file name. This code sets the selected file name as a TextBox.Text property.
 
private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "File Upload";
            fdlg.Filter = "Xml File|*.xml";
            //fdlg.FilterIndex = 2;
            fdlg.RestoreDirectory = true;
            Nullable<bool> result = fdlg.ShowDialog();
            if (result==true)
            {
                txtFileUpload.Text = fdlg.FileName;
            }
        }