Click here to Skip to main content
15,881,380 members

How to add the column width dynamically for gridview in asp.net

CH Guravaiah asked:

Open original thread
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.
Tags: ASP.NET4.0

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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