Click here to Skip to main content
12,953,263 members (82,538 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
How 2 Fetch Data in GRID Control using ADO.NET in WPF...???
Posted 7-Dec-12 19:57pm
Comments
Tadit Dash 8-Dec-12 2:07am
   
Do you mean DataGrid ?

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

WPF Database connectivity

USING GRID CONTROL

<window x:class="testWPF.MainWindow" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" WindowStartupLocation="CenterScreen" BorderThickness="1" BorderBrush="#FF190000">
<grid name="grdSample" verticalalignment="Center" horizontalalignment="Center" height="auto" width="auto">
<grid.columndefinitions>
<columndefinition width="200"></columndefinition>
<columndefinition width="200"></columndefinition>
</grid.columndefinitions>
 
<grid.rowdefinitions>
<rowdefinition height="30"></rowdefinition>
<rowdefinition height="30"></rowdefinition>
<rowdefinition height="30"></rowdefinition>
<rowdefinition height="30"></rowdefinition>
<rowdefinition height="30"></rowdefinition>
<rowdefinition height="30"></rowdefinition>
<rowdefinition height="30"></rowdefinition>
<rowdefinition></rowdefinition>
</grid.rowdefinitions>
 
<textblock grid.column="0" grid.row="0" horizontalalignment="Center" removed="Aqua">Field Name</textblock>
<textblock grid.column="1" grid.row="0" horizontalalignment="Center" removed="Aqua">Field Value</textblock>
 
<textblock grid.column="0" grid.row="1">First Name</textblock>
<textblock grid.column="0" grid.row="2">Last name</textblock>
<textblock grid.column="0" grid.row="3">Address</textblock>
<textblock grid.column="0" grid.row="4">Email</textblock>
<textblock grid.column="0" grid.row="5">Country</textblock>
 
<textbox name="txtFname" grid.column="1" grid.row="1"></textbox>
<textbox name="txtlname" grid.column="1" grid.row="2"></textbox>
<textbox name="txtAdd" grid.column="1" grid.row="3"></textbox>
<textbox name="txtEmail" grid.column="1" grid.row="4"></textbox>
<textbox name="txtCountry" grid.column="1" grid.row="5"></textbox>
 
<button name="btnInsert" content="Insert" verticalalignment="Center" grid.column="0" grid.row="6" click="btnInsert_Click"></button>
<button name="btnClear" content="Clear All" verticalalignment="Center" grid.column="1" grid.row="6" click="btnClear_Click"></button>
 

<listview name="lvSample" grid.column="0" grid.columnspan="2" grid.row="7" itemssource="{Binding Path=Table}">
<listview.view>
<gridview x:name="gvsample">
<gridviewcolumn width="80" header="First Name" displaymemberbinding="{Binding Path=Fname}" />
<gridviewcolumn width="80" header="Last Name" displaymemberbinding="{Binding Path=Lname}" />
<gridviewcolumn width="100" header="Address" displaymemberbinding="{Binding Path=Address}" />
<gridviewcolumn width="80" header="Email" displaymemberbinding="{Binding Path=Email}" />
<gridviewcolumn width="60" header="Country" displaymemberbinding="{Binding Path=Country}" />
</gridview>
</listview.view>
</listview>
 
</grid>
 

</window>


In the Code part of the application, Simply loading the grid to display the existing record. Ans in the Insert button the code to insert the record is placed.. Define your own connection string create a SAMPLE data base as i have used and the table name to insert the record is TEST. Please take a look at the coding part.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;
 
namespace testWPF
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
#region "Global Variable"
string strConnString = "Data Source=ServerName;Initial Catalog=Sample;uid=sa;pwd=test;Pooling=true;Min Pool Size=0;Max Pool Size=1000;";
 
#endregion
 
public MainWindow()
{
InitializeComponent();
txtFname.Focus();
loadGrid();
}
 
private void btnInsert_Click(object sender, RoutedEventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
string strQuery = "insert into test values('" + txtFname.Text.Trim() + "','" + txtlname.Text.Trim() + "','" + txtAdd.Text.Trim() + "','" + txtEmail.Text.Trim() + "','" + txtCountry.Text.Trim() + "')";
SqlCommand cmd = new SqlCommand(strQuery, con);
int a = cmd.ExecuteNonQuery();
if (a > 0)
{
MessageBox.Show("Record Inserted!");
clearAll();
loadGrid();
}
else
{
MessageBox.Show("Error while insertion.");
//clearAll();
}
con.Close();
}
 
private void btnClear_Click(object sender, RoutedEventArgs e)
{
clearAll();
}
private void clearAll()
{
txtFname.Text = "";
txtlname.Text = "";
txtAdd.Text = "";
txtEmail.Text = "";
txtCountry.Text = "";
txtFname.Focus();
}
 
private void loadGrid()
{
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter ad = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
con.Open();
string strQuery = "select * from Test";
cmd.CommandText = strQuery;
ad.SelectCommand = cmd;
cmd.Connection = con;
DataSet ds = new DataSet();
ad.Fill(ds);
lvSample.DataContext = ds.Tables[0].DefaultView;
con.Close();
}
}
}
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,429
CHill60 3,490
Maciej Los 3,103
Jochen Arndt 1,975
ppolymorphe 1,920


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 8 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100