.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

I need help please : how do i bind dataGrid to dataTable Programmatically ?

Posted By:      Posted Date: September 29, 2010    Points: 0   Category :WPF

Hi all,


please i need to know if i could  bind dataGrid to dataTable Programmatically using wpf V.S 2010 (without using Xaml code)???


i google it before but i saw solution using (DataSet) and this not useful for me because its huge application.

Note : data source is MySql.


Thanks a lot .


Abed Salem

View Complete Post

More Related Resource Links

How add and bind DataGrid columns programmatically ??


Hi all,


How are you ??


please i need to know how to add and bind DataGrid columns programmatically ( NOT IN XAML),

i was using  this code in windowsForms i need equivalent for it in WPF please :

            DataGridViewColumn column = new DataGridViewTextBoxColumn();
            column.DataPropertyName = "DataPropertyName";
            column.Name = "columnName";
            column.DisplayIndex = 1;
            column.Width = 150;
            column.ReadOnly = true;
            column.Visible = true;


help me please , Thanks a lot .....






How to add validation rule programmatically on datagrid column, whose itemsource is datatable?

I want as user ends editing a datagrid cell, a validation rule should be applied on this cell for error checking like whether its value is null or not and in case of null it should display the error on that cell. All this i want to do programmatically not in xaml. Can anybody guide me with an example ?

How to bind GridView and DropDownList controls using DataTable object

In .net framework 2.0, you can load a DataTable in memory by consuming a data source using a DataReader. In this version you will find the flexibility in accessing the DataReader means write access code once and use again and again using DataReader. To see the functionality of DataTable consider an example that is created using VS2005.

Binding DataGrid to empty DataTable

Hi, I'm very much new to WPF and am finding it a bit tricky to find my way in the mass of (not always great) documentation. Right now I'm struggling to get the grid to display it's UI when bound to an empty datatable. In my XAML: <DataGrid Name="grid" ItemsSource="{Binding}" />  In my code-behind (MainWindow constructor, following InitializeComponent): var dt = new DataTable(); dt.Columns.Add("col1"); dt.Columns.Add("col2"); //var r = dt.Rows.Add("toto", "tata"); grid.DataContext = dt; If I comment out the line that adds a row to the table, all is well -- the grid generates columns and displays UI. But if the table is empty, the grid renders a thin horizontal white rectangle with no column names and it doesn't seem possible to interact with this at all. As it happens, I want the grid to be empty by default, so I need to somehow get the grid to play along. If anyone knows why this happens and what to do to solve it, I'd much appreciate the help!

read delimited files into a datatable and bind to a gridview

I have a delimted file , like a csv file but the delimters are # and not ,I assume I would use SqlConnection and SqlDataAdapterSo for a csv file what is the sequence to read in for example c:\data\demo.csvWhat would the connection string be.Also can you specifiy a different delimiter.The code below is what I assume it would possibly look likea although its only a frst guess. ThanksJim Dim connectionString As String = "Data Source=localhost;Initial Catalog=master;Integrated Security=true;" Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("Northwind_ConnectionString").ConnectionString) Dim str As String = "SELECT * FROM Employees" Dim cmd As New SqlCommand(str, con) Dim da As New SqlDataAdapter(cmd) Dim dt As New DataTable() da.Fill(dt) GridView1.DataSource = dt GridView1.DataBind()

How to set row Background color for WPF DataGrid Programmatically (WPF & C#) based on the value in t



I have a WPF Datagrid binded with list of interface objects.
Consider, ClsEmployee class implements I_Employee interface with properties Empl_Id, Empl_Name, Department, Address and City_name.

List<I_Employee> _emplList;

consider, _emplList has 10 items.

dgEmployeeGrid.ItemsSource = _emplList;

Now, if the user clicks on a button, then i should be able to read the City_name. Based on the City_name, i should be able to set the color (Color can be different for each row) for the rows dynamically through C# code.

Please help me how to do this?

Thanks in advance!

How to bind datagrid columns to external (main) datacontext?



I have a datagrid and his columns binds well to its collection data. But when I add a new data column (ie. DataGridTextColumn or DataGridComboBoxColumn) and Bind to the main Data Context of window the data are missing (dont show)

In this sample, The last two columns of DataGrid dont bind to the main data.

<Window x:Class="MainWindow"





Bind a GridView to a DataTable (in Viewstate) with full in-line CRUD


Hello fellow devs!

I need a bit of help with a scenario. I am working on a web application that requires huge amounts of data to be Added, Deleted, and Updated. The data entry forms are divided into logical groups through Multiviews. All the information is saved when the mighty Finish button is pressed. The current setup (previous developer) does not allow me to use transactions. Therefore, if I am to save a new Courier to the database, I need to add his/her Distance and Rate info. In addition, I need to add his/her Banned Areas info (Area Name, Post Code).

This is where it gets interesting. Obviously, the DistamceAndRate table and the BannedArea table in my SQL Server will have the CourierID as a foreign key. Since I'm going to save the Courier as well as the Rates and Areas info in one go, I cannot have the newly created CourierID before. Therefore, I cannot bind my Grids for Distance + Rates and Banned Areas directly to database.
What I am doing is creating two DataTables and managing them in Viewstate through properties as follow:

        private DataTable TableDistancesAndRates {
            get {
                object objTableDistancesAndRates = ViewState["vTableDistancesAndRates"];

                if (objTableDistancesAndRates!=null) {
                    return (DataTable)objTableD

datatable bind to datagridview

i have a datatable with 3000 records, when i bind to datagridview, it takes a long time to display record, how to improve this so that the datagridview can be displayed faster

DataGrid and DataTable



In my WPF application, I want to create an Empty Table.

I've created an empty DataTable (created columns and rows) and i want to display it using DataGrid.

I can't make the DataGrid to work together with my DataTable...

(i'm creating my DataTable via code and not via XAML).

can anyone help?




how to bind panel control to datatable


i have panel & i m trying to bind it with data table

as panel has no datasource property. can anyone solve it out.

DataTable dt=new DataTable ();

DataRow dr = dt.NewRow();
            dr["serviceid"] = ddlservices.SelectedValue.ToString();
            foreach (DataRow row in dt.Rows)

thanks in advance

Bind ComboBox in DataGrid row



I want to bind ComboBox to database table (via ADO.NET Entity Framework) and put it in DataGrid. Now I have DataGrid with binded TextBlock, but when I try to bind ComboBox it doesn't work.


I have table with list if countries and table users, where each user has foreign key of country where he lives.

I want to show in DataGrid user details like:

Alex Gorr     Canada     al.gorr@email.yo


And when I click on country field I want to see ComboBox with list of countries from table countries.

How can I do it?


how to add datatable in to datagrid?


how can i bind datatable to datagrid by using dataGrid.DataContext .

just in c#code i mean no xaml code.

How to bind DataTable to ObjectDataSource



is it possible to bind DataTable to ObjectDataSource? 


DataGrid/DataTable: Deleting Added Item Issue



I’ve a DataGrid bound to a DataTable, and I’ve a problem deleting rows programmatically.

If I add a new row (using UI) and I mark the row with an error (using SetColumnError), and press the Delete key, the new row is deleted, everything is ok.

If I delete the new row (with errors) programmatically, the row is deleted, but no rows appear in the grid. There isn’t way to add a new row anymore, the grid has no items.

If I delete the new row (programmatically) and it hasn’t errors everything is ok. The row is deleted, and a new empty row appears in the grid. I can add more rows correctly.

programmatically add/edit datagrid rows



I'm new to WPF, and I'd like to add rows with data at runtime, and edit data manually in a DataGrid, just as it was possible to do with the WinForms DataGridView.

WPF DataGrid control is really awesome, but lacks some simplicity and generality of WinForms, like unbound data...

Can someone please give me a hint?



create a checkbox programmatically in VB.NET for a datagrid view


My goal is to  that contains a splitter

and 2 DataGridview controls .  The first datagridview is used as a header only and the second datagridview

contains the rows and cell that will be populated with data.  This composite usercontrol replicates a datagrid

because I needed a caption property.


Although the datagridview has a checkbox column, I was not able to figured out how can I change the location of

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend