C# .NET - databinding in datagrid view in wpf - Asked By srini vasan on 26-Dec-11 06:58 AM

how to bind data to textbox from datagrid view in wpf through  c# code.
dipa ahuja replied to srini vasan on 26-Dec-11 07:03 AM
Step 1 : Write this code in .cs
 
public Form1()
{
  InitializeComponent();
 
  string connString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True";
 
  SqlDataAdapter da = new SqlDataAdapter("Select * from emp1", connString);
  DataSet ds = new DataSet();
  
  da.Fill(ds);
  dataGrid1.DataContext = ds;    
}
Step 2 : set the properties of dataGrid this way:
 
<DataGrid  ItemsSource="{Binding Path=Tables[0]}"   
    Margin="10"
    x:Name="dataGrid1"   
    ColumnHeaderHeight="30" AutoGenerateColumns="True"   
    CanUserSortColumns="False">
</DataGrid>
 
 
Riley K replied to srini vasan on 26-Dec-11 07:12 AM


You can get the cell value like this.


<StackPanel>
      <dg:DataGrid Name="dataGrid1" ItemsSource="{Binding}" CurrentCellChanged="dataGrid1_CurrentCellChanged">
 
      </dg:DataGrid>
      <TextBox Name="textBox1"></TextBox>
      <TextBox Name="textBox2"></TextBox>
    </StackPanel>
 
public partial class Window1 : Window
  {
    private DataTable dt = new DataTable();
    public Window1()
    {
      InitializeComponent();
      dt.Columns.Add("Column1");
      dt.Columns.Add("Column2");
      dt.Rows.Add(1, "Item1");
      dt.Rows.Add(2, "Item2");
      dt.Rows.Add(3, "Item3");
 
      dataGrid1.DataContext = dt;
    }
 
    private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
    {
      DataRowView drv = dataGrid1.CurrentCell.Item as DataRowView;
      if (drv != null)
      {
        textBox1.Text = drv.Row[0].ToString();
        textBox2.Text = drv.Row[1].ToString();
      }
    }
  }

Regards
Jitendra Faye replied to srini vasan on 26-Dec-11 07:14 AM

Try like this-

<!-- defines a custom column -->
<dg:DataGridTemplateColumn Header="Date Of Birth" >
    <!-- editing template -->
    <dg:DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <dg:DatePicker
               SelectedDate="{Binding Path=DateOfBirth, Mode=TwoWay}"
               BorderThickness="0"/>
        </DataTemplate>
    </dg:DataGridTemplateColumn.CellEditingTemplate>
    <!-- standard template -->
    <dg:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock>
                <TextBlock.Text>
                    <Binding Path="DateOfBirth"
                       ConverterCulture="en-GB"
                       StringFormat="{}{0:d}"/>
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>
    </dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>



Follow this link also-
http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx
Sri K replied to srini vasan on 26-Dec-11 07:40 AM

Add the UpdateSourceTrigger to your Binding

Text="{Binding ElementName=dataGridVendors, Path=SelectedItem.Name, Mode=TwoWay,  UpdateSourceTrigger=PropertyChanged}"

Its default trigger is lost focus. When you change it to PropertyChanged the Updates will done when you're typing.