Posted by: Nishant Rana | September 21, 2007

Dynamically adding rows and columns to Grid View

Hi to add rows and columns to Gridveiw dynamically what
we will do is that

1) Create a DataTable object to which than we will bind the GridView
DataTable dt=new DataTable();

2) IF you need two columns than create two DataColumn object
DataColumn dCol1=new DataColumn(FirstC, typeof(System.String)); // string FirstC=”column1″
DataColumn dCol2=new DataColumn(SecondC, typeof(System.String)); // string SecondC=”column2″

Add it to the table

dt.Columns.Add(dCol1);
dt.Columns.Add(dCol2);

3)Run the loop for as many rows you want to add.

// say you want to add two rows

for(int i=0;i<2;i++)
{
DataRow row1 = dt.NewRow();
row1 [FirstC] = “One”;
row1 [SecondC] =”Two”
dt.Rows.Add(row1 );

}

Now iterate through each datacolumn and create a BoundField foreach column

foreach (DataColumn col in dt.Columns)
{
BoundField bField = new BoundField
bField.DataField = col.ColumnName;
bField.HeaderText = col.ColumnName;
GridView1.Columns.Add(bField);
}
GridView1.DataSource = dt;
//Bind the datatable with the GridView.
GridView1.DataBind();

Responses

Hello,

Adding Cols & rows Dynamically in the GridView is good one.

But here I m facing one problem.

As I have DataTable dt with the column as follows

dt["Sales-1001"] = 85
dt["Expiry-1001"] = 90

dt["Sales-1002"] = 66
dt["Expiry-1002"] = 44

Now I have bind the columns as u told above
but here I want the Column.Header text is different.

so I did :

foreach (DataColumn dcol in dt.Columns)
{
BoundField bField = new BoundField();
bField.DataField = dcol.ColumnName;
bField.HeaderText = dcol.ColumnName.ToString().Substring(0, dcol.ColumnName.LastIndexOf(’-'));
GridView1.Columns.Add(bField);
}

GridView1.DataSource = dt;
GridView1.DataBind();

But is is giving me double Columns Attached to GridView.
Which have header Sales,Expiry,Sales,Expiry,Sales-1001,Expiry-1001,Sales-1002,Expiry-1002 like this

it is showing boud field name too in the header.

what to do..Suggest

Tnx.

Just Include the lign below befopre setting the datasource.

GridView1.AutoGenerateColumns = false;

While creating a dynamic gridview you might have left the autogeneratecolumns property to true, which actually renders all the columns of the of the source datable irrespective of any bound columns created during runtime.

I hope it works for you.

Happy Programming!
Arvind
HookLogic Inc

How to add Template Fields in GridView?
Bound Fields is OK but I wanna add ItemTemplateField. Please . . .

string[] fields = new string[] { “Icon”, “Name”, “ToolTip”, “Document”};

foreach (string field in fields)
{
TemplateField customField = new TemplateField();
customField.ShowHeader = false;
customField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, field, tempGrid.ID, this.Page);
tempGrid.Columns.Add(customField);
}

You have to implement your own GridViewTemplate class, send me a mail if you want mine.

Leave a response

Your response:

Categories