Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF
How 2 Fetch Data in GRID Control using ADO.NET in WPF...???
Posted 7-Dec-12 19:57pm
Comments
Tadit Dash at 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">
 
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
0 Sergey Alexandrovich Kryukov 271
1 OriginalGriff 223
2 nv3 50
3 VJ Reddy 45
4 Mehdi Gholam 45
0 Sergey Alexandrovich Kryukov 6,696
1 OriginalGriff 6,149
2 CPallini 2,473
3 Richard MacCutchan 1,697
4 Abhinav S 1,560


Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 8 Dec 2012
Copyright © CodeProject, 1999-2014
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