how to bind data in datagrid in wpf

Posted by Shanky11 under WPF on 8/23/2013 | Points: 10 | Views : 1170 | Status : [Member] | Replies : 3
this is my code................
public void fillgrid()
{
SqlDataAdapter da =new SqlDataAdapter ();

DataTable dt = new DataTable();
dt.Columns.Add("Event", typeof(Int32));
dt.Columns.Add("Notified by", typeof(string));
dt.Columns.Add("Result", typeof(string));
dt.Columns.Add("Date", typeof(string));

DataRow dtrow = dt.NewRow(); // Create New Row
dtrow["Event"] = 1; //Bind Data to Columns
dtrow["Notified by"] = "SureshDasari";
dtrow["Result"] = "B.Tech";
dtrow["Date"] = "Chennai";
dt.Rows.Add(dtrow);
da.Fill(dt);

//dtrow = dt.NewRow(); // Create New Row
//dtrow["Event"] = 2; //Bind Data to Columns
//dtrow["Notified by"] = "MadhavSai";
//dtrow["Result"] = "MBA";
//dtrow["Date"] = "Nagpur";
//dt.Rows.Add(dtrow);

}




Responses

Posted by: Bandi on: 8/23/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Table structure in the database is as follws:
TableName: STATE

Columns are:
STATE_CD
STATE_NAME

Design: MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="455" Width="592" Loaded="Window_Loaded">
<Grid>
<DataGrid AutoGenerateColumns="False" Height="344" HorizontalAlignment="Left" Margin="141,43,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="338">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding STATE_CD}" Width="100" Header="State Code"/>
<DataGridTextColumn Binding="{Binding STATE_NAME}" Width="100" Header="State Name"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>



-- MainWindow.xaml.cs
using System;

using System.Configuration;
using System.Data;
using System.Data.SqlClient;
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;

namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
FillDataGrid();
}

private void FillDataGrid()
{
//string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
string CmdString = string.Empty;
using (SqlConnection con = new SqlConnection("Data Source=ServerName; User Id=testLogin;Password=XXXX@1234; Initial Catalog=DBName;"))
{
CmdString = "SELECT * FROM STATE";
SqlCommand cmd = new SqlCommand(CmdString, con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable("States");
sda.Fill(dt);
dataGrid1.ItemsSource = dt.DefaultView;
}
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
FillDataGrid();
}
}
}



Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanky11 on: 8/23/2013 [Member] Starter | Points: 25

Up
0
Down
i am saking how tio assign the whole dt in datagrid
in dt value is there becauz i am generating the dynamic datatable
see my abovw code
i am saking only that how to bind the dt to datagrid

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 8/23/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
public void FillDataGrid()

{
SqlDataAdapter da = new SqlDataAdapter();

DataTable dt = new DataTable();
dt.Columns.Add("Event", typeof(Int32));
dt.Columns.Add("NotifiedBy", typeof(string));
dt.Columns.Add("Result", typeof(string));
dt.Columns.Add("Date", typeof(string));

DataRow dtrow = dt.NewRow(); // Create New Row
dtrow["Event"] = 1; //Bind Data to Columns
dtrow["NotifiedBy"] = "SureshDasari";
dtrow["Result"] = "B.Tech";
dtrow["Date"] = "Chennai";
dt.Rows.Add(dtrow);

//dtrow = dt.NewRow(); // Create New Row
//dtrow["Event"] = 2; //Bind Data to Columns
//dtrow["Notified by"] = "MadhavSai";
//dtrow["Result"] = "MBA";
//dtrow["Date"] = "Nagpur";
//dt.Rows.Add(dtrow);
dataGrid1.ItemsSource = dt.DefaultView;
}

Call above binding method in the Window_Loaded() method and MainWindow() constructor

-- Design should be
    <Grid>

<DataGrid AutoGenerateColumns="False" Height="140" HorizontalAlignment="Left" Margin="141,43,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="338">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Event}" Width="100" Header="Event"/>
<DataGridTextColumn Binding="{Binding NotifiedBy}" Width="100" Header="NotifiedBy"/>
<DataGridTextColumn Binding="{Binding Result}" Width="100" Header="Result"/>
<DataGridTextColumn Binding="{Binding Date}" Width="100" Header="Date"/>
</DataGrid.Columns>
</DataGrid>
</Grid>


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response