Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET4.0
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.
 

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 6-Jun-13 23:39pm
Edited 6-Jun-13 23:46pm
v7
Comments
MAKReddy at 7-Jun-13 4:50am
   
Hi Can U Upgrade Your Question ......

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
Take knowledge from this below code :
 

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[^]
  Permalink  
v3

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

  Print Answers RSS
0 Afzaal Ahmad Zeeshan 259
1 Maciej Los 240
2 BillWoodruff 240
3 OriginalGriff 220
4 Sergey Alexandrovich Kryukov 195
0 OriginalGriff 6,419
1 Sergey Alexandrovich Kryukov 6,008
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,070


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 7 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100