.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

How add and bind DataGrid columns programmatically ??

Posted By:      Posted Date: October 02, 2010    Points: 0   Category :WPF

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 .....






View Complete Post

More Related Resource Links

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"





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


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


the database result is as follows DATE     code   class1 class2  class3 1/8/10     M1     10       20     30     3/8/10     V2      20       30     10      1/8/10      H1      1           2      3         7/8/10     ! ! 8/8/10     !M2    5      5      5       3/8/10     B2      4      4     4       1/8/10     V1      3      7     3       THE OUTPUT SHOULD BE AS FOLLOWS:- DATE           M1                          M2           

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 Selective Columns from Stored Proc to Gridview using the entity framework


Hi Guys,

I have this entity model>> http://img840.imageshack.us/img840/306/schemaj.jpg and I would like to bind  a list  which consists of: employee's

  • emloyee_firstname(employees table) + employee_lastname(employees table)
  • employee's extension(employee_extension table)
  • deparment name(departments table)

to my gridview. I already created my stored procedure

    Concat(employees.employee_firstname," ",employees.employee_lastname) as name
    , extensions.extension_number
    , departments.department_description
    INNER JOIN employees 
        ON (employee_extensions.employee_id = employees.employee_id)
    INNER JOIN extensions 
        ON (employee_extensions.extension_id = extensions.extension_id)
    INNER JOIN departments 
        ON (employees.department_id = departments.department_id)

But my problem is, i don't know how to bind it since i don't have an entity that contains the employee's name, extension# and departments name. Do I need to create a custom class that consists of said columns? or is their a proper way to do this? I'

Sort WPF Datagrid using multiple columns



Is it possible to sort a WPF datagrid using more than one column ?



how to hide a column in a datagrid but autogenerated columns = "True"?



  Anybody help me please!!! I've 3 datasets named ds1, ds2, ds3. Then i merged that 3 datasets into 1 datatable named dt1. Now, my question is, i want to hide some datacolumns its present in an ds2. How????? Below i attached my code,



connectionString = "provider=MSDAORA;data source=proact;user id=mah;password=mah";


OleDbConnection myOleDbConnection = new OleDbConnection(connectionString);


Datagrid with varying columns


I am upgrading a classic asp page, the page uses response.write to make a table, as soon as a null value comes up for a month the columns of the grid then stop.  The sql is below

SELECT sName, Strategy, 
Sum(case when Month([Date])=1 then Rtn else null end) as Jan, 
Sum(case when Month([Date])=2 then Rtn else null end) as Feb, 
Sum(case when Month([Date])=3 then Rtn else null end) as Mar, 
Sum(case when Month([Date])=4 then Rtn else null end) as Apr, 
Sum(case when Month([Date])=5 then Rtn else null end) as May, 
Sum(case when Month([Date])=6 then Rtn else null end) as Jun, 
Sum(case when Month([Date])=7 then Rtn else null end) as Jul, 
Sum(case when Month([Date])=8 then Rtn else null end) as Aug, 
Sum(case when Month([Date])=9 then Rtn else null end) as Sep, 
Sum(case when Month([Date])=10 then Rtn else null end) as Oct, 
Sum(case when Month([Date])=11 then Rtn else null end) as Nov, 
Sum(case when Month([Date])=12 then Rtn else null end) as Dec  
FROM Chess

If this is run in oct i only have data from jan -0ct , how do i not display nov and dec so that its dynamic?

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?


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

I declared a datagrid in xaml, now I want to add main columns in this datagrid at runtime through vi


I have declared datagrid in xaml like this ;

<DataGrid AutoGenerateColumns="False" ........


and want to access this datagrid in viewmodel to add columns with headers to this datagrid at runtime. Actually i want to declare any number of columns in grid which i read from database table at execution time. If anybody could help me in this regard....

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 ?

Can a .sql file be bind into a datagrid or something else in WPF?


I am making a program, and want to add a .sql file to it.

And don't really know what I am doing to be honest.

Any help would be great



MS SQL Server: Search All Tables, Columns & Rows For Data or Keyword Query

If you need to search your entire database for specific data, this query will come in handy.

So when a client needs a custom report or some sort of custom development using Great Plains, most of the time I will have to track down the data in the system by running this query and find the table(s) it is in.

How to format and update GridView and DataGrid rows using JQuery

The behavior described in this question is as expected. When you set text of a cell in grid, it directly affects HTML that is going to be rendered. When you set text value of a cell, it means that you are setting innerText of the cell. The column that GridView creates for command fields (Edit, Delete and Select) are a (anchor) or button elements. So you can see what will happen if you set text value in that cell. It will wipe out those link or button controls and replace them with simple text string.

Use Jquery with repeater, gridview, datagrid

Before we talk about jquery inside any itemtemplate (repeater, gridview, datagrid), we need to understand the basic use of jquery functions
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