Click here to Skip to main content
15,895,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends , I have a requirement on gridview, i have a datatable for that i was assigning the column names manually and also adding the data to datatable. After that i am set the datatable vertically and bind to the gridview.


C#
var tbl = dt;


          var swappedTable = new DataTable();
          if (tbl.Rows.Count > 0)
          {
              swappedTable.Columns.Add("Name");

              for (int i = 0; i <= tbl.Rows.Count-1; i++)
              {


                  DataRow dx = tbl.Rows[i];


                  var x = dx.ItemArray;

                 // swappedTable.Columns.Add("Value"+i.ToString());



                  swappedTable.Columns.Add(x[3].ToString());



              }
              for (int col = 0; col < tbl.Columns.Count; col++)
              {
                  var r = swappedTable.NewRow();
                  r[0] = tbl.Columns[col].ToString();
                  for (int j = 1; j <= tbl.Rows.Count; j++)
                      r[j] = tbl.Rows[j - 1][col];


                  swappedTable.Rows.Add(r);
              }
              //dataGridView1.DataSource = swappedTable;

              gvwHDMonitor.DataSource = swappedTable;
              gvwHDMonitor.DataBind();


Example :


Name ...................... IICSS21

Operating System ............. Microsoft Windows 7 Professional
Version .............. 6.1.7601
Manufacturer ............. Microsoft Corporation
Computer Name ............. IICSS21
Windows Directory ............. C:\Windows
Serial Number ............. 00371-OEM-9203635-37779
ComputerManufacturer Name ............. Dell Inc.
Computer Model ............. OptiPlex 990
System Type ............. x64-based PC


here : Operationg system, Version like these are the columns
Microsoft Windows 7 Professional,6.1.760l these are the first row in datatable and same time other records also added dynamically to datatable as columns.



Here dynamically added the columns to datatable.
Problem is i want to increase the column width for dynamically added columns.
How can do this problem.

Please any one help me.
Posted
Updated 6-Jun-13 22:46pm
v7
Comments
MAKReddy 7-Jun-13 4:50am    
Hi Can U Upgrade Your Question ......

1 solution

Hi,

Take knowledge from this below code :


C#
protected void GridView1_RowDataBound(object sender,
    GridViewRowEventArgs e)
{
    System.Data.DataRowView drv;
    drv = (System.Data.DataRowView)e.Row.DataItem;
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
      if (drv != null)
      {
        String catName = drv[1].ToString();
        Response.Write(catName + "/");

        int catNameLen = catName.Length;
        if (catNameLen > widestData)
        {
          widestData = catNameLen;
          GridView1.Columns[2].ItemStyle.Width =
            widestData * 30;
          GridView1.Columns[2].ItemStyle.Wrap = false;
        }

      }
    }
}


If not solved, check here :http://msdn.microsoft.com/en-us/library/ms178296(v=vs.100).aspx[^]


If you want to wrap the cell content then check here : http://myaspsnippets.blogspot.in/2011/01/there-are-occasions-when-one-may-work.html[^]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900