Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8767339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:47:04+00:00 2026-06-13T16:47:04+00:00

I have a DataGrid which gets its data from an sql table. I have

  • 0

I have a DataGrid which gets its data from an sql table. I have set AutoGenerateColumns to true.

When I click a button the data in the DataGrid is exported to a pdf-file.

However when the width of all columns combined in the DataGrid is bigger than about 800 the table is bigger than the page inside the pdf.

I have set the DataGrid’s MaxWidth-Property to 800. When I resize a column I can drag the cursor outside of the DataGrid and a horizontal scrollbar appears.

Is there a way to limit the maximum size of all columns combined to 800 so that I can not make the column bigger than the DataGrid?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:47:06+00:00Added an answer on June 13, 2026 at 4:47 pm

    The easy solution is to make the DataGrid use starsizing for its columns by simply setting the ColumnWidth property like this:

    <DataGrid Width="800" ColumnWidth="*" />
    

    The problem however, is that this makes all columns equally wide, which is probably not desired.

    So what I would do, is to first create all columns the default way like you do now, and then set each column to a starsize afterwards. That way a value can be calculated for the new width, that makes the column keep its initial size.

    I have written a small piece of code, to show what i mean..

    XAML

    <Window x:Class="WpfApplication2.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfApplication2"
            Title="MainWindow" Height="350" Width="850">
        <StackPanel>
            <DataGrid x:Name="dgTest" Width="800" Loaded="dgTest_Loaded" />
        </StackPanel>
    </Window>
    

    Code-behind

    public partial class MainWindow : Window
    {
        public class Test
        {
            public string P1 { get; set; }
            public string P2 { get; set; }
            public string P3 { get; set; }
        }
    
        public MainWindow()
        {
            InitializeComponent();
    
            var t = new List<Test>(new[] { 
                new Test{ P1="på,dsl", P2="234234", P3="asdasdasd"},
                new Test{ P1="asasaspå,dsl", P2="23sadasd asf afasdasdasd4234", P3="asdasdasd" }, 
                new Test{ P1="på,ds1231l", P2="234", P3="1ddsdasd" },
            });
    
            dgTest.ItemsSource = t;
        }
    
        private void dgTest_Loaded(object sender, RoutedEventArgs e)
        {
            //Make the columns use starsizing so their combined width
            //can't be bigger than the actual datagrid that contains them.
            foreach (var column in dgTest.Columns)
            {
                var starSize = column.ActualWidth / dgTest.ActualWidth;
                column.Width = new DataGridLength(starSize, DataGridLengthUnitType.Star);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have defined an XMLList which gets its data from the following XML file:
I have a datagrid which populated by some data from sqlserver.I have datetime filed
I have a datagrid which gets data like this: public struct MyData { public
I have a DataGrid in a WPF application which has for its ItemsSource a
I have Datagrid which is clicking by mouse in each row is showing data
I have a datagrid, which gets filled by an ArrayList. I want one column
I have a DataGrid which I am binding to a PagedCollectionView which is grouped
I have a DataGrid which is being bound dynamically to a database query. The
I have a DataGrid which i am using on the page alogn with 4
I have a datagrid view which is editable. I'm getting the value of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.