C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ASP.NET WebForms »

Dynamically creating controls(for example checkbox on datagrid) without using Template Column


Posted Date: 02 Jul 2008    Resource Type: Code Snippets    Category: ASP.NET WebForms
Author: FahedMember Level: Bronze    
Rating: 1 out of 5Points: 7



By Creating the checkboxes dynamically in datagrid by using Template Column,checkboxes with same contents are repeating in all rows.So i want to restrict the checkboxes(one or more) in cells(0),cells(1),cells(2) etc...So instead of using itemtemplte,we can create the checkbox dynamically by the following way....Enjoy the coding.....Cheers

BindGrid()
{
Declare an sqlconnection and connect it to the database and fill it in dataset by using sqldataadapter

DataTable dttable = new DataTable();

for (int i = 0; i < ds.Tables["main_type"].Rows.Count; i++)
{
for (int k = 0; k < ds.Tables["sub_type"].Rows.Count; k++)
{
if (Convert.ToInt32(ds.Tables["sub_type"].Rows[k]["type_id"]) == Convert.ToInt32(ds.Tables["main_type"].Rows[i]["type_id"]))
{
if (!dttable.Columns.Contains(ds.Tables["main_type"].Rows[i]["tx_type"].ToString()))
{
dttable.Columns.Add(new DataColumn(ds.Tables["main_type"].Rows[i]["tx_type"].ToString()));
}
}
}
}

DataRow drNewRow = dttable.NewRow();
dttable.Rows.Add(drNewRow);

///bind it to the datagrid

}

public int total_count = 0;
public int cell_count = 1;
protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{

if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
if (total_count == 0)
{
for (int i = 0; i < ds.Tables["main_type"].Rows.Count; i++)
{
total_count = i;
if (ds.Tables["main_type"].Rows[i]["type_id"] != null)
{
for (int k = 0; k < ds.Tables["sub_type"].Rows.Count; k++)
{
if (Convert.ToInt32(ds.Tables["sub_type"].Rows[k].ItemArray[0]) == Convert.ToInt32(ds.Tables["main_type"].Rows[i]["type_id"]))
{
CheckBox valCheck = new CheckBox();
///For identifying which checkbox is clicked
///you can use valcheck.ID = ....
valCheck.Text = ds.Tables["sub_code"].Rows[k].ItemArray[1].ToString() + "
";
e.Item.Cells[cell_count].Controls.Add(valCheck);
}
}
cell_count++;
}
}
}
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Filter the characters in a string
Previous Resource: Bind DropDownList Through AJAX
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET WebForms


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use