Problem with Table columns Anchor property

Posted on 2005-04-23
Last Modified: 2009-07-29
I have a " DataGrid " and its source is a " DataTable ".
this DataTable have two " DataColumn ".
I want to create Anchor for my Table columns such that
when user enlarge form, the datagrid and its columns be enlarged.
datagrid.Anchor work properly, but I could not create anchor
for Table columns; I test following way :

DataGridTextBoxColumn myColumnStyle1 ;
// some codes
myColumnStyle1.TextBox.Anchor =    // set proper anchor

but it dosen't work.
How I can solve this problem.

Question by:mr60
    1 Comment
    LVL 13

    Accepted Solution

    You can do something like that:

    private void BindGrid()
                //string strSql = "SELECT CityID, City.Code AS CityCode, City.Name AS CityName, Country.Code AS CountryCode, Country.Name AS CountryName FROM MST_City AS City JOIN MST_Country AS Country ON City.CountryID = Country.CountryID";
                string strSql = "Select FirstName, LastName From Employees";
                SqlConnection cn = new SqlConnection(CONNECTION_STRING);
                SqlDataAdapter adapter = new SqlDataAdapter(strSql, cn);
                DataTable tblCity = new DataTable("Employee");
                dgrdData.DataSource = tblCity;
                DataGridTableStyle tsl = new DataGridTableStyle();
                tsl.MappingName = tblCity.TableName;

                DataGridColumnStyle txtCol = new DataGridTextBoxColumn();
                txtCol.MappingName = "FirstName";
                txtCol.HeaderText = "First Name";
                // create new grid column style
                DataGridColumnStyle txtCol1 = new DataGridTextBoxColumn();
                txtCol1.MappingName = "LastName";
                txtCol1.HeaderText = "Last Name";

                // width of each column
                int intWidth = 0;
    if ((dgrdData.TableStyles.Count > 0) && (dgrdData.TableStyles[0].GridColumnStyles.Count > 0))
                                  intWidth = Convert.ToInt32(Math.Round(Convert.ToDouble(dgrdData.Width / dgrdData.TableStyles[0].GridColumnStyles.Count)));
                foreach (DataGridColumnStyle colStyle in tsl.GridColumnStyles)
                      colStyle.Width = intWidth;
                // add the DataGridTableStyle object to the collection
          catch (Exception ex)
    private void dgrdData_Resize(object sender, System.EventArgs e)
    int intWidth = 0;
                      if ((dgrdData.TableStyles.Count > 0) && (dgrdData.TableStyles[0].GridColumnStyles.Count > 0))
                            intWidth = Convert.ToInt32(Math.Round(Convert.ToDouble(dgrdData.Width / dgrdData.TableStyles[0].GridColumnStyles.Count)));
          foreach (DataGridColumnStyle colStyle in dgrdData.TableStyles[0].GridColumnStyles)
                colStyle.Width = intWidth;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now