C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » WPF »

How to bind the data to datagrid in wpf in c#


Posted Date: 15 Oct 2009      Posted By: Gaddamchandrakanth      Member Level: Silver     Points: 1   Responses: 2



HI THIS IS FROM CHANDRAKANTH.

I AM WORKING ON WPF PROJECT.
MY QUESTIONS IS I WANT TO BIND THE DATA TO DATAGRID.
ACTUALLY I AM GETTING DATA INTO DATATABLE.
WHEN I GO FOR .....
DATAGRID.ITEMSOURCE = DS.TABLES[0].TOSTRING();
IT IS NOT BINDING.

HERE I AM GIVING XAML CODE AND CODEBEHIND CODE.PLEASE HAVE LOOK ONCE AND TELL ME WHAT IS THE PROBLEM.

--------------XMAL CODE------------------------
<Page x:Class="ZenASM_WPFClient.Module.Groups_Module.PgeDefaultGroups"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfToolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
Background="DarkSlateGray"
Title="PgeDefaultGroups" Width="600" Height="392" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit">
<StackPanel x:Name="DefaultGrpsGrid" Height="680" Width="600" HorizontalAlignment="Left" VerticalAlignment="Top" >
<Grid x:Name="grdDefaultGrps" Width="600" Height="289" HorizontalAlignment="Left" VerticalAlignment="Top" >
<WpfToolkit:DataGrid x:Name="dgDefaultGrps"
ItemsSource="{Binding}"
HorizontalScrollBarVisibility="Hidden"
CanUserAddRows="False"
CanUserResizeRows="False" CanUserSortColumns="False"
AutoGenerateColumns="True" RowHeaderWidth="17" RowHeight="25">

<WpfToolkit:DataGridTextColumn Header="GID" Width="1*" Binding="{Binding Path=GID}"/>

<WpfToolkit:DataGridTextColumn Header="GROUPNAME" Width="1*" Binding="{Binding Path=GROUPNAME}"/>

<WpfToolkit:DataGridTextColumn Header="GROUPDESCRIPTION" Width="1*" Binding="{Binding Path=GROUPDESCRIPTION}"/>

</WpfToolkit:DataGrid>

</Grid>

</StackPanel>

</Page>
--------------XMAL CODE------------------------

-------------C# CODE----------------------------
public void BindDefaultUserGroups(DataTable dtTmp)
{
DataSet ds = new DataSet();
ds.Clear();
ZENASMService zenGrps;
zenGrps = new ZENASMService();
ds = zenGrps.GetUserDefaultGroups();
dgDefaultGrps.AutoGenerateColumns = true;
dgDefaultGrps.ItemsSource = ds.Tables[0].ToString();
this.DataContext = ds.Tables[0].ToString();

}

HERE I AM GETTING DATA INTO DATATABLE BUT UNABLE TO BIND THE DATA.
-------------C# CODE----------------------------


THANKS AND REGARDS
CHANDRAKANTH









Responses

Author: seema    15 Oct 2009Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx
http://www.c-sharpcorner.com/UploadFile/mahesh/WpfDGP109272009111405AM/WpfDGP1.aspx



Author: ABitSmart    16 Oct 2009Member Level: DiamondRating: 2 out of 52 out of 5     Points: 2

Since, you are binding to the ItemsSource you need to set the DataContext like this,

dgDefaultGrps.DataContext = ds.Tables[0];


You could also set the ItemsSource directly in code,

dgDefaultGrps.ItemsSource = ds.Tables[0].DefaultView;


Kind regards,
ABitSmart
DNS Web-master, DNS MVM
My blog
Thoughts.exe



Post Reply
You must Sign In to post a response.
Next : Graphical library in wpf
Previous : How to handle wpf datagrid row double click event?
Return to Discussion Forum
Post New Message
Category: WPF

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use