What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 15796 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > WPF > how to bind the data from database to gridview and select a specified value from a column ...
Raj_Chennai

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

 Code Snippet posted by: Raj_Chennai | Posted on: 3/17/2012 | Category: WPF Codes | Views: 1271 | Status: [Member] | Points: 40 | Alert Moderator   


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:


>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/24/2013 3:40:34 AM