• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 369
  • Last Modified:

Problem with Table columns Anchor property

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.

1 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;

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now