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

Problem with Table columns Anchor property

Hi.
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.

THANK YOU.
0
mr60
Asked:
mr60
1 Solution
 
dunglaCommented:
You can do something like that:

private void BindGrid()
{
      try
      {
            //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");
            adapter.Fill(tblCity);
            dgrdData.DataSource = tblCity;
            DataGridTableStyle tsl = new DataGridTableStyle();
            tsl.MappingName = tblCity.TableName;

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

            // 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
            dgrdData.TableStyles.Add(tsl);
      }
      catch (Exception ex)
      {
            MessageBox.Show(ex.Message);
      }
}
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;
      }
}
0

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