Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF c#, bind datagrid column with code behind

Tags:

c#

wpf

datagrid

I have question how i can bind datagrid column to collection?

 dataGrid1.Columns.Add(new DataGridTemplateColumn { Header="d", Binding = "RoomNumber"}); 

binding does not exsists what can i use to bind?

it work perfectly but i need to bind it with code behind

 <DataGridTextColumn Header="Room Number" Binding="{Binding RoomNumber}"/>
like image 929
Irakli Lekishvili Avatar asked Jul 30 '11 20:07

Irakli Lekishvili


People also ask

Is WPF and C# same?

C# is a programming language. WPF is a technology used to develop rich GUI applications using C# (or any other . NET language).

Is WPF still supported 2022?

What's New in WPF Version 4.5 | Microsoft Learn. This browser is no longer supported. Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Is WPF used anymore?

However, some industries still rely on WPF concepts and tools like XAML & MVVM to develop futuristic web, mobile, and desktop applications. None of Microsoft's GUI frameworks ever really die. WPF has top-notch performance, with a high level of customization, and if you aim for Windows, both WPF is critical.

Is WPF part of C#?

WPF is used to build Windows client applications that run on Windows operating system. WPF uses XAML as its frontend language and C# as its backend languages. WPF was introduced as a part of . NET Framework 3.0 as the Windows library to build Windows client apps and the next generation of Windows Forms.


1 Answers

As far as i understand you want to add data grid column from code behind and that column should work with binding..?

here is sample snippet to add datagrid column from code behind

 var col = new DataGridTextColumn();
            col.Header = "d";
            col.Binding = new Binding("RoomNumber");
            dataGrid1.Columns.Add(col);

With this approach you can add as many columns as you want and you can give data binding at run time for each column and you can specify itemssource at once....

make sure to mark AutoGenerateColumns="False" in your data grid so that you can avoid unwanted columns get added from itemssource..

like image 110
Bathineni Avatar answered Sep 17 '22 22:09

Bathineni