Search
MVC Online Training
Winners

Win Prizes

Social Presence
Twitter Twitter LinkedIn YouTube Google

Like us on Facebook
Advertisements
Top Codes Author
Wed, 17-Sep-2014 Authors
All Time Authors
Satyapriyanayak
12160
Jayakumars
8120
Niladri.biswas
6000

Latest members | More ...


(Statistics delayed by 5 minutes)
 Codes

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 on 3/17/2012 2:30:03 AM | Points: 40 | Views : 2367 | Status : [Member]

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();
}


raj
Found interesting? Add this to:



Comments or Responses


Login to post response