Dynamically generating gridview with the values from database

Posted by Shivnaik under C# on 3/12/2011 | Points: 10 | Views : 6807 | Status : [Member] | Replies : 3
Hello,

I have stuck with an issue in my project. So please help me

A brief description about what I have done till now:

There are two drop down lists in my page. The first lists the projects. On selection of a project, the second drop down populates based on the project id. When a second drop down is selected I

am calling a stored procedure by passing the selected item value. The stored procedure returns me a table with the values in the following format.

ID Name colname1 colname2 colname3 colname4 . . . . . colnameN
1 feature1 0 0 1 0 1
1 feature2 0 1 0 1 0
1 feature3 0 0 1 0 1
.
.
.
1 featureN 0 1 0 1 0



The number of columns is not know until run time and is depending on the ID passed by the second drop down list. The first column will be always ID, and second column will be always strings and the remaining columns will be either 0 or 1. I need to create a user interface with the above structure in a tabular form replacing the 0s and 1s with check box controls either unchecked(if 0) or checked(if 1). The users will modify the check boxes( check or uncheck) and click on update button. I need to update the database by mapping the values in the UI. i.e. I have four columns in DB with ID, Name, ColumnName and selected. I need to identify that for what feature and for what column the checkbox is checked or unchecked and update the values appropriately.
I am using a grid view and dynamically generating the columns in the code behind using TemplateField class. I am inheriting the ITemplate and generating the checkboxes and displaying in the grid.

Now the problem is on form submission I am unable to get the values of checkboxes in code behind for updating database. I am using
CheckBox cb = (CheckBox)GridView1.Rows[rowcount].Cells{columnCount].FindControl("chk");
but this cb is always null. May be because the checkbox Id are assigned automatically(this is came to know by viewing the page source) as the check boxes are generated dynamically.

Please help me how to find the check box status in code behind after update button is clicked. Is there any other way of doing this other than using Gridview???




Responses

Posted by: Shivnaik on: 3/12/2011 [Member] Starter | Points: 25

Up
0
Down
This is the code snippet for your reference

DataTable dt = new DataTable();

dt = FillGridViewDataTable();

//Add the columns to the gridview by creating BoundField dynamically
int colCount = 0;
foreach (DataColumn dColumn in dt.Columns)
{
if (colCount >= 2)
{
//int test = (int)dt.Rows[colCount].ItemArray[dt.Columns.IndexOf(dColumn)];
//bool status = test.Equals(0) ? false : true;

TemplateField TmpCol = new TemplateField();
TmpCol.HeaderText = dColumn.ColumnName;
TmpCol.ItemTemplate = new TemplateHandler(status);
GridView1.Columns.Add(TmpCol);
}
else
{
BoundField bField = new BoundField();
bField.HeaderText = dColumn.ColumnName;
bField.DataField = dColumn.ColumnName;
GridView1.Columns.Add(bField);
GridView1.DataBind();
}
colCount++;
}


//Hide the box-id column
GridView1.Columns[0].Visible = false;

//Assign the values to the gridview from the datatable
GridView1.DataSource = dt;
//Finally Bind the datatable values to the gridview
GridView1.DataBind();


public class TemplateHandler : ITemplate
{
public bool checkSts = false;

public TemplateHandler(bool status)
{
checkSts = status;
}
void ITemplate.InstantiateIn(Control container)
{
CheckBox cBox = new CheckBox();
cBox.Checked = checkSts;
cBox.ID = "chk";
container.Controls.Add(cBox);
}

}


Shivnaik, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shivnaik on: 3/12/2011 [Member] Starter | Points: 25

Up
0
Down
Please reply :-(

Shivnaik, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 3/15/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Check if this link could help you

http://www.codeproject.com/KB/aspnet/dynamic_Columns_in_Grid.aspx

Thanks
Karthik
www.f5Debug.net

Shivnaik, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response