how to bind the data from database to gridview and select a specified value from a column

Raj_Chennai
Posted by Raj_Chennai under WPF category on | Points: 40 | Views : 4538
provide this content in a xaml :
 <Grid>
<DataGrid Background="{x:Null}" BorderBrush="#FFE21414" FontSize="14" FontStretch="Expanded" FontStyle="Italic" FontWeight="Medium" Foreground="#FFDE2828" Height="119" HorizontalAlignment="Left" Margin="77,32,0,0" Name="dgvPath" VerticalAlignment="Top" Width="326" AutoGenerateColumns="True" ItemsSource="{Binding}" />
<GroupBox Header="user" Height="128" HorizontalAlignment="Left" Margin="146,162,0,0" Name="groupBox4" VerticalAlignment="Top" Width="171" Foreground="#FFDE2525" BorderBrush="#FFDE1A1A">
<StackPanel DataContext="{Binding ElementName=dgvPath,Path=SelectedItem}">
<Grid Height="117">
<Label Content="{Binding Path=name}" Foreground="#FFD81E1E" Height="28" HorizontalAlignment="Left" Margin="45,20,0,0" Name="lblSlave" VerticalAlignment="Top" Width="61" FontFamily="Script MT" FontSize="12" FontStretch="Expanded" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="45,64,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</StackPanel>
</GroupBox>
</Grid>

where <StackPanel DataContext="{Binding ElementName=dgvPath,Path=SelectedItem}"> in which dgvPath refers to datagridview name and <Label Content="{Binding Path=name}" Foreground="#FFD81E1E" Height="28" HorizontalAlignment="Left" Margin="45,20,0,0" Name="lblSlave" VerticalAlignment="Top" Width="61" FontFamily="Script MT" FontSize="12" FontStretch="Expanded" /> in which Content="{Binding Path=name}" in which name refers to column name from which the value is selected

use this in buttonclick event:
SqlConnection con = new SqlConnection("Data Source=raj=pc\\SQLEXPRESS;Initial Catalog=sampleDB;Integrated Security=True");
private void button1_Click(object sender, RoutedEventArgs e)
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from complaintsdetails",con);
DataSet ds = new DataSet();
da.Fill(ds, "ss");
this.DataContext = ds.Tables["ss"];
con.Close();
}

Comments or Responses

Login to post response