This is a discussion on ASP.NET Data Controls within the ASP and ASP.NET Programming forums, part of the Web Development category; hi guys one more data bound control in asp.net.... GridView Control The GridView control displays data as a table ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
|
#11
| |||
| |||
| hi guys one more data bound control in asp.net.... GridView Control The GridView control displays data as a table and provides the capability to sort columns, page through data, and edit or delete a single record. |
|
#12
| |||
| |||
| Hi all How can I add a checkbox to each row of a GridView dynamically? I realise that this can be done by using <ItemTemplate></ItemTemplate> in the aspx page, but I'm trying to have columns in the GridView corresponding to different datasets. //-- foreach (DataColumn dc in ds.Tables[0].Columns) { BoundField bField = new BoundField(); bField.HeaderText = dc.ColumnName; bField.DataField = dc.ColumnName; GridView1.Columns.Add(bField); } //-- I want a checkbox appended to each of the rows in the Gridview as well. How can I do that? Any pointers would be appreciated. Thanks
__________________ G.A.P |
|
#13
| |||
| |||
| hi You could add code to your gridview's DataBound event: protected void gvData_DataBound(object sender, EventArgs e) { foreach (GridViewRow objRow in gvData.Rows) { TableCell tcCheckCell = new TableCell(); CheckBox chkCheckBox = new CheckBox(); tcCheckCell.Controls.Add(chkCheckBox); objRow.Cells.AddAt(0, tcCheckCell); } } |
|
#14
| |||
| |||
| Hi, if you need this checkbox to appear for each DataSet, you can still use ItemTemplate in your GridView declaration. Then you can add your dynamically created columns. However, if you only need this checkbox is some cases, you needto add TemplateField column to your grid and assign ItemTemplate property. You need to have instance of a class implementing ITemplate interface to assign it to ITemTemplate property. To get this instanace you can: 1. Create a UserContorl with CheckBox inside and load it using LoadTemplate method of your page 2. Use sample class and code from: It Could Be Done!: Implementing ITemplate as Anonymous Method 1. This sample uses Button instead of CheckBox, just need to replace 2. This sample demonstrates implementation of ITemplate for GridView column Note, that if you have ViewState enabled for your GridView, you will probably lose your checkbox on postback. So, this simple TemlateField sample works only for GridViews with disabled ViewState. If data binding on each postback is not acceptable for you, please ask more questions here.
__________________ J Suresh Kumar Google Hacks ![]() |
|
#15
| |||
| |||
| Hi garunprasad, Why cant you try another approach. give an itemTemplate with checkbox in each row. make it visible or invisible according to your need.(according to DataSet) Regards,
__________________ H2O Without us, no one can survive.. |
|
#16
| |||
| |||
| Hi guys Thanks for the replies. I managed to add a check box using an approach similar to the one suggested by JSuresh, but involved creating a class extending INamingContainer, ITemplate. Found this link really helpful: WEBSWAPP Development Inc. However, I'm having trouble getting to this control by using GridViewRow's FindControl method. For some reason, it does not locate the control in the row with the ID that has been allocated to the checkbox. Here is my code: //-- foreach (GridViewRow gvr in GridView1.Rows) { CheckBox chkSelect = (CheckBox)gvr.FindControl("chkUpdate"); if (chkSelect != null && chkSelect.Checked) { gvr.Cells[5].Text = ddlChangeStatus.SelectedValue; DataRow dr = ds.Tables[0].Rows[gvr.DataItemIndex]; dr["Status"] = ddlChangeStatus.SelectedValue; } } //-- It returns a null for that particular control. Any suggestions?? Thanks
__________________ G.A.P |
|
#17
| |||
| |||
| Hi, I am not sure how is INamingContainer used on template class, but I think it is unrelated. 1. When do you add your new columns and assign ItemTemplate property? Is it page_load, page_init or somewhere else? 2. Do you have ViewState enabled on your GridView?
__________________ J Suresh Kumar Google Hacks ![]() |
|
#18
| |||
| |||
| Hi, The columns are being added elsewhere, as you put it. I might be doing it wrong altogether, but when the gridview is displayed, the checkboxes have an ID similar to "#GridView_ctl01_chkUpdate" (as viewed using the web developer toolbar in Firefox). But I'm looking only for "chkUpdate" using the FindControl method. What exactly should I be looking for in the FindControl method? Would it be something similar to "#GridView_ctl01_chkUpdate"? perhaps I could use something like a regular expression to search for the checkbox.
__________________ G.A.P |
|
#19
| |||
| |||
| Hi, IDs are correct. ctl01 is ID of the GridViewRow and it looks for its child controls with correct prefixes. Depending on answers to previous questions, I may be able to explain what is the problem.
__________________ J Suresh Kumar Google Hacks ![]() |
|
#20
| |||
| |||
| Hi, Are you defining the name of your checkbox explicitly? It is not enough to just define the item, you need to set its ID property or it won't be found by findcontrol: CheckBox chkMyBox = new CheckBox(); chkMyBox.ID = "chkMyBox"; |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| silverlight controls for .net | stevejhon | ASP and ASP.NET Programming | 1 | 03-25-2009 10:31 AM |
| silverlight controls for .net | stevejhon | ASP and ASP.NET Programming | 0 | 03-23-2009 01:55 AM |
| How can we Dynamically create web controls | Kirubhananth | ASP and ASP.NET Programming | 3 | 03-14-2008 09:00 PM |
| WebParts controls | S.Vinothkumar | ASP and ASP.NET Programming | 5 | 11-16-2007 02:06 AM |
| Asp.net controls not supported by Ajax? | Gopisoft | ASP and ASP.NET Programming | 0 | 07-16-2007 11:22 PM |
Our Partners |