.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

WPF DataGrid Column Width issue

Posted By:      Posted Date: September 07, 2010    Points: 0   Category :WPF
I have a simple window with 2 tabs, and 4 datagrids.  I set up each one with similar, if not identical settings.  They all get their own DataSet based binding, and have standard Text columns, and combo box columns.  Recently (past week) of development, while I was adding filtering/sorting/grouping features to my program, the column widths have begun to ingore my settings.  By collapsed I mean approx 20 pixels wide (just enough for the sorting Icon). Contacts datagrid, works great in run time, but in design time the columns are all collapsed. ContactsToClients data grid has no abnormal issues Clients datagrid AND Clients to Contacts datagrid look great in design time, but show collapsed columns in run time. How do I hunt down the problem?  I've tried removing all the temporary code, and rebuilding, closing Visual Studio and reopening.  I've remmed out the sorting code.  None of these have any affect.  It appears that my settings are all being ignored.  Here is the XAML code:   <Window x:Class="wdContacts" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:my

View Complete Post

More Related Resource Links

WPF DataGrid specify column width from code behind


I have DataGrid, initially I create a DataTable with column headers and bind this to DataGrid. when user click on refresh button I will bring data and bind it again.

In the first step, I will create columns in DataTable with no rows and bind that to datagrid to just to display column headers with empty rows.

DataGrid1.DataContext = DataTable1.DefaultView;

DataGrid1.ItemsSource = DataTable1.DefaultView;

when i try to get the columns count here, it comes as zero. As this data changes everytime based on user click, I have to specify column width in code behind.

Can anyone help me how to assign column width from code behind so that we can adjust column spacing for better user view.




Datagrid: Rendering issue with Star-Column




if i define a usercontrol with datagrid on it, which has one star column in the middle, it has a ugly rendering issue when its resized/shown. it seems that the calculation of star column's width is asynchronous started in OnRender.

here is the repro xaml code:


<UserControl x:Class="DatagridTest.UserControl1"
       d:DesignHeight="300" d:DesignWidth="300">

  <Grid DataContext="{Binding Source={StaticResource SampleDataSource}}">
    <DataGrid x:Name="dGrid" ItemsSource="{Binding Collection}" AutoGenerateColumns="False" RowHeight="40" >
        <DataGridTemplateColumn Width="100">
                <Label Content="{Binding Property1}" Height="30"/>

Set column width of data bound WPF dataGrid


Hello all,


I successfully created dataGrid that is bound to a class, so it automatically generates 4 columns.

The problem is that now I can not set the columns width, and there is always a 5th column which has no data, and fills most of the space.

How can i hide this 5th column, and adjust the width of the other ones?




Has the extra "filler" column issue of DataGrid been addressed?

When DataGrid uses auto-generated columns (i.e. default behavior), an extra "filler" column is generated as discussed here last year.  I am wondering if this has been fixed or it is by-design feature.

Multiple Column Dropdownlist for the ASP.NET DataGrid

Based on my previous control "Multiple Column DropDownList for ASP.NET", I received many emails asking for the same control to be used in the DataGrid for web applications. Here we go.. This control can be used as the regular MS DropDownList in the DataGrid and also as a regular dropdownlist. It has all the properties, like DataTextField, DataValueField, DataSource, SelectedIndex etc. The download file contains the samples both in VB.NET and C#. In this sample, I have used the Northwind database of SQL Server.

DataGrid: Tailor Your DataGrid Apps Using Table Style and Custom Column Style Objects


One of the most enduring challenges in writing user interfaces is figuring out how to display large amounts of data efficiently and intuitively without bewildering the user. The problem becomes particularly thorny when the interface must reflect hierarchical relationships within the data that the user needs to modify. The Windows Forms DataGrid control gives developers a powerful and flexible tool to meet this challenge. This article explains its basic operations and shows how to extend the DataGrid to display columns of data in an application-appropriate manner.

Kristy Saunders

MSDN Magazine August 2003

Datasheet view lookup column issue


When a column that has a lookup is empty in datasheet view and I attempt to update a different column I get this message: the text entered for isn't an item from the list. select an item from the list, or enter text that matches one of the listed items.

I found this hotfix, but it did not apply.


Any ideas how to get this resolved?

WPF Datagrid Combobox column

Hi, I am creating a WPF Datagrid with the combobox column in it. All the datagrid binding and combobox column binding works fine without any problem. But, when I select a value in the combobox column and move to the next column or row, the value is reset to the previous empty selection. Am I missing anything here? Please find the code snippet that I have used below. <dg:DataGridComboBoxColumn x:Name="technicianname" Header="Assigned To" ItemsSource="{Binding}"/> technicianname.ItemsSource = dtTechnicianInfo.DefaultView; technicianname.DisplayMemberPath = "TechnicianName"; technicianname.SelectedValuePath = "TechnicianEid"; Thanks in Advance!!! Best Regards, Subalakshmi Vijayarajan.

How to scroll the DataGrid (horizontally) when user drags a column header far left or right?

We're working with the WPF DataGrid and I have a question about  dragging column headers: The grid lets the user rearrange the columns, by dragging the column header left or right.  So far, so good. But, sometimes we have many columns, and only the first few columns are visible; so there is a horizontal scrollbar.  In such case, if the user drags a column header to the right edge of the DataGrid window, I want the DataGrid to scroll, automatically, one column at a time.  Is there an easy way to do that? (To be clear: yes, the user can already scroll horizontally by interacting with the horizontal scrollbar, but that isn't what I'm asking about.  I'm asking if the "drag a column header left or right" gesture can cause the DataGrid to scroll left or right.) I guess I could handle various low-level events, but it seems like it shouldn't be necessary -- DataGrid has so much functionality built-in, that I'm guessing it already has the ability to scroll when the user drags the column header to the edge.  So, is there either (a) a property that I can set, to tell the DataGrid to scroll when the user drags a column header to the edge of the window;  or (b) a code example showing how to achieve this effect myself?  Thanks!

WPF Datagrid with multiple row selection/checkbox column template

Hello, I am using the WPF Toolkit Datagrid with a checkbox defined in a template column/template cell. We are using the checkbox template so that selecting the checkbox will also highlight the row in the datagrid. Here is the template definition: <dg:DataGridTemplateColumn MinWidth="50" Width="Auto" Header="" CanUserReorder="False"> <dg:DataGridTemplateColumn.CellTemplate> <DataTemplate> <CheckBox IsChecked="{Binding Path=IsCheckedm, Mode=TwoWay}" /> </DataTemplate> </dg:DataGridTemplateColumn.CellTemplate> </dg:DataGridTemplateColumn> We are allowing multiple row selection in the datagrid and using MVVM (using MVVM Light). We would like to keep a list in the View Model of selected rows as they are selected/unselected. Any suggestions on how we can leverage MVVM to keep the list of selected/unselected elements current. Many Thanks --Sam

can do rowspan+columnspan in DataGrid Column Headers?

i wanna do something like this: 4 column headers: Header Font, Header Size, Cell Font, Cell Size you can see they are 2 grps, with a pair of each. now i wanna create the column headers like:     Header  |     Cell Font | Size  | Font | Size is it possible? and how to do that?

How to set the width of column on GridView

How to set the specific the width of each column, the columns are created automaticly 

Accessing Combobox inside WPF Datagrid Template Column

Hi, I am creating a WPF datagrid template column with combobox. The problem that I face now is, I am not able to bind a table to the combobox inside the WPF datagrid template column. Here is the code snippet. <dg:DataGrid x:Name="Students" AutoGenerateColumns="False" Loaded="OnLoad"> <dg:DataGrid.Columns> <dg:DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" Width="75"/> <dg:DataGridTemplateColumn x:Name="Dept"> <dg:DataGridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox x:Name="DeptId" ItemsSource="{Binding}" SelectionChanged="Dept_SelectionChanged" DisplayMemberPath="{Binding Path=DeptName}" SelectedValuePath="{Binding Path=DeptId}" SelectedItem="{Binding Path=Belongs}"/> </DataTemplate> </dg:DataGridTemplateColumn.CellTemplate> </dg:DataGridTemplateColumn> </dg:DataGrid.Columns> </dg:DataGrid> Please help me to find out the way to assign a dynamic source to the combobox through code behind file. Thanks in Advance!!! Best Regards, Subalakshmi Vijayarajan.

Weird Date vs Date/Time issue using a calculated column

I'm attempting to use the fab 40 attendance template. I don't need the time to show - Im able to hide that on the forms with jquery (endusersharepoint.com thank you!!) I WAS ATTEMPTING to create a calculated column called Start Date where the formula simply reads '=[Start Time]' When it's set to display 'Date Only' the date is off by a day. If I switch it to 'Date & Time' I get the correct date. Huh?

Sort a WPF DataGrid on a Customized Column

I have several wpf DataGrids that use something similar to: {     Binding Path=ContactID, Converter={StaticResource contactIDToNameConverter}}"       As a best concept for various ID fields in my database.  This is great when needing to display editable data that is linked by ID, and my converter handles the validation and cleanup if needed. Otherwise an SQL View, or Joined Table SQL would not be able to send updates directly to the database (DataSet Wizard).  For this DataGrid's Contact column (bound to the ContactID field from a ICollectionView; bound to a DataSet.ClientsToContacts DataTable), I have an underlying value of Integer, and a converted surface value of String.  I want to sort on the string. Using ICollectionView.View.SortDescription(path,direction) will only sort on the Integer, and I am desprately seeking work arounds.  Please help. jvj

How to sum a column in a datagrid for just the multiselected rows?

I have a WPF4 datagrid, populated via linq to sql, which has some numeric fields. How do I sum the value of one of these fields for just the selected rows? (multiselect is turned on)  
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