Simple DataGrid

Feb 16, 2009 at 1:41 PM
Edited Feb 16, 2009 at 1:44 PM
Hi all;

I am using datgrid in WPFtoolkit 2008 toolbar, i just want to populate it with data from database. here is scenario:

There is one textbox and a button, user enter details in text box and press that button and it should display data on datgrid based on text enetered. 

here is xaml code:

 <Window x:Class="Registrant.Details"

 

 

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

 

 

 

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

 

 

 

xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"

 

 

 

Title=" Registration System - Details " Height="895.475" Width="1000" Loaded="Window_Loaded">

 

 

 

<Grid>

 

 

 <

Label Height="28" HorizontalAlignment="Left" Margin="33,32,0,0" Name="label1" VerticalAlignment="Top" Width="120">Enter Practitioner ID</Label>

 

 

 

<TextBox Height="23" HorizontalAlignment="Left" Margin="187,32,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />

 

 

 

<Button Height="23" HorizontalAlignment="Left" Margin="349,32,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click">Go</Button>

 

 

 

<dg:DataGrid HorizontalAlignment="Left" Margin="33,0,0,551" Name="dataGrid1" Width="391" Height="200.475" VerticalAlignment="Bottom"  />

 

 

 

</Grid>

 

 

 </ 

 

Window>

 

 

 

here is code behind.

 

 

 

 

using 

 

System.Data.OleDb;

 

 

 

using System.Data.Sql;

 

 

 

using System.Data;

 

 

 

namespace Registrant

 

 

 

{

 

 

 

public partial class Details : Window

 

 

{

 

 

public Details()

 

{

InitializeComponent();

 

}

 

 

private void Window_Loaded(object sender, RoutedEventArgs e)

 

{

}

 

 

 

private void button1_Click(object sender, RoutedEventArgs e)

 

{

 

 

OleDbConnection myConn = new OleDbConnection(

 

 

 

@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\App_Data\\Database2.mdb");

 

 

 

OleDbDataAdapter adapter = new OleDbDataAdapter();

 

 

 

OleDbCommand OurCommand;

 

OurCommand =

 

new OleDbCommand("Select * From registrant where (regtype Like '%" + textBox1.Text.ToString() + "%')", myConn);

 

 

 

 

try

 

 

{

myConn.Open();

OurCommand.ExecuteNonQuery();

myConn.Close();

}

 

catch (OleDbException a)

 

{

 

 

Console.WriteLine("Error: {0}", a.Errors[0].Message);

 

}

 

}

}

}

Another question what should come after ItemsSource property of datagrid.



Any help much appreciated.

thanks

 

Feb 16, 2009 at 7:57 PM
Hi,

The ItemsSource property of your datagrid should be set to the collection of objects which you wish to view in the grid.

I demonstrate some simple examples of populating a DataGrid from a database in my codeproject article:

http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx

hope that helps.




Regards,
Colin E.
--
http://www.scottlogic.co.uk/blog/wpf/ - my WPF blog
http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx - WPF DataGrid Practical Examples
Feb 17, 2009 at 9:28 AM
Thanks Colin,

I was just wondering if i can get a simple example of using dataGrid with Access database using Oledb. I am not too familiar with how to fill adapter and dataset. what is difference between them?

I read your posting over and over but cant get simple idea, it would be great if someone send simple example according to above scenario.

Any help
Much Appreciated.


Thanks
Feb 18, 2009 at 10:04 AM

Hi,

I have figured it out, actually it is very simple and i have just done it by using datatable and OleDbReader.

Here is sample code:

private void button1_Click(object sender, RoutedEventArgs e)

 

{

 

OleDbConnection myConn = new OleDbConnection(

 

 

@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\App_Data\\Database2.mdb");

 

 

OleDbCommand myCommand;

 

myCommand =

new OleDbCommand("Select * From registrant where registrantid =" + textBox1.Text.ToString() , myConn);

 

 

DataTable dt = new DataTable();

 

 

 

try

 

{

myConn.Open();

 

OleDbDataReader reader;

 

reader = myCommand.ExecuteReader();

dt.Load(reader);

dataGrid1.ItemsSource = dt.DefaultView;

myConn.Close();

}

 

catch (OleDbException a)

 

{

 

Console.WriteLine("Error: {0}", a.Errors[0].Message);

 

}

 

 

}


Thanks..