Cannot Add a blank Row to databound datagrid with Checkbox control

Hello,

I think I am posting this question again but I'm quite frustrated as I haven't found any way around.

I have a databound datagrid in which New/Blank rows are added to this datagrid on a button click event. This button is outside the datagrid and not in the footer. The datagrid populates well and I can do the edit/update to the already created records. I recently had to add a new checkbox field to my data table. This chekcbox field is to be included in this datagrid. The chkbox works fine for previously created records but as soon as I hit the 'Add New Row' button I get the following error :

<i>Exception Details: System.InvalidCastException: Conversion from type 'DBNull' to type 'Boolean' is not valid.

Source Error:
Line 1176:<asp:templatecolumn headertext="Current Driver?">
Line 1177:<itemTemplate>
Line 1178:<asp:checkbox id="cbcurrentdriver" runat="server" autopostback="true" enabled ="false" checked='<%# DataBinder.Eval(Container.DataItem,"currentdriver")  %>' /></itemTemplate></i>

My code for adding a new/blank row (on button click event) is as below:
<i>RefreshGrid()
Dim ds as DataSet = DataGrid.datasource
Dim blank as DataRow = ds.tables(0).NewRow()
   
ds.tables(0).Rows.insertAt(blank, ds.tables(0).Rows.Count)
DataGrid.EditItemIndex = ds.tables(0).Rows.Count-1
DataGrid.DataBind()</i>

and my checkbox template column is as below:

<i><asp:templatecolumn headertext="Current Driver?">
<itemTemplate>
<asp:checkbox id="cbcurrentdriver" runat="server" autopostback="true" enabled ="false" checked='<%# DataBinder.Eval(Container.DataItem,"currentdriver")  %>' /></itemTemplate>
<headerstyle width="8%" />
</asp:templatecolumn></i>

I really am clueless on this one. everything seems fine. Nulls are allowed on this field in my datatable.Please help!

Thanks.


Aartee
...we live more than once!
0
arty
2/27/2007 4:51:35 AM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

3 Replies
607 Views

Similar Articles

[PageSpeed] 51

Dear friend:

Make sure that there is no NULL value in the currentdriver field.

Or you can add a helper function to check whether the field contains a null field and return a default value.

checked='<%#SomeFunction( DataBinder.Eval(Container.DataItem,"currentdriver") ) %>'

 

I hope the above information will be helpful. If you have any issues or concerns, please let me know. It's my pleasure to be of assistance

 


Best Regards,
__________________________________________________
Sincerely,
Rex Lin
Microsoft Online Community Support

If there is any question or the issue is not resolved, please feel free to mark the thread as not resolved
0
Rex
2/27/2007 8:03:11 AM

Hi Rex,

Thankyou for your time to go through my post.

Well I added the following in my Itemtemplate:

<

itemtemplate>

<asp:checkbox id="cbcurrentdriver" runat="server" autopostback="true" checked='<%# Iif(DataBinder.Eval(Container.DataItem,"currentdriver")= System.DBNull, false, DataBinder.Eval(Container.DataItem,"currentdriver")) %>'

enabled="false" />

</itemtemplate>

but I get this error: BC30691: 'DBNull' is a type in 'System' and cannot be used as an expression.

I also tried '==' instead of '=' but with that I get the error  : BC30201: Expression expected.
I have also tried using 'Ctype','Convert.boolean' everything but it just does not create a blank row for me. It was working absolutely fine until I added the checkbox Template column.

Any other ideas on how I can make it work?

Thanks.

 


Aartee
...we live more than once!
0
arty
2/27/2007 11:24:08 PM

Dear, friend:

This works for me. You can have a look at this snippet:

public static object ValueOrNull(object value){
    return (object == DBNull.Value? null, value);
}

<asp:checkbox id="cbcurrentdriver" runat="server" autopostback="true" checked='<%# Convert.ToBoolean(ValueOrNull(DataBinder.Eval(Container.DataItem,"currentdriver"))%>'

I hope the above information will be helpful. If you have any issues or concerns, please let me know. It's my pleasure to be of assistance


Best Regards,
__________________________________________________
Sincerely,
Rex Lin
Microsoft Online Community Support

If there is any question or the issue is not resolved, please feel free to mark the thread as not resolved
0
Rex
3/1/2007 3:37:13 AM
Reply:

Similar Artilces:

Add new controls to a gridview row; when editing that row using the edit row feature in the gridview control.
Hi all  I have a questions that looks hard / complex to me, and i know you can help me in this. Q {  I have row in a Gridview and i have an edit option to it. I press the edit button, to edit the values in that row and update data all the way back to the database. Now when i press the edit button, i want a drop down list to be displayed that contain values for a particular field in my grid view. (for now only plain text boxes appear.) } I hope you understand my question, all i need to do is replace the text box controls with a dropdown list when i am editing a row in ...

this.Controls.Add does not add control to child collection (this.Controls.Count == 0)
Ok, as silly as it may sound, I have a situation where I am creating a CompositeControl in ASP.NET 2.0, C#. I have the following code in the CreateChildControls() method that build the control's child control collection: 1    for (int i = 0; i < _menus.Count; i++)2    this.Controls.Add(_menus[i]);3    4    foreach (FloatingMenu menu in _menus)5    {6    Label lbl = new Label();7    lbl.ID = string.Concat("GroupQ", menu.ID);8    lbl.CssClass = "menuIt...

Add .Net Control to Web Custom Control
 I want to add .NET Control (like: TextBox, Button...) to Web Custom Control.How i can do it? Alphavn That's called a composite control -- "Mark As Answer" if my reply helped you -- Hello Guys What gunteman said, that is one approach and in most cases the right one.But its also possible to add child controls to a Control thats inherit from WebControl itself. Be sure to implement INamingContainer to that inherited Control too, thats the important point of that approach.--Microsoft MVP - Visual Developer ASP / ASP.NET- http://www.aspnetzone.de/blogs/peterbuch...

How to place a DataGrid Control with in a DataGrid Control
Hi, I want to place a DataGrid control in a DataGrid control can any one plz tell me the procedure to place the control Thanks in Advance Krisamigo <!---<asp:DataGrid ID=dg_List runat=server><Columns><asp:TemplateColumn><ItemTemplate><asp:DataGrid ID=dg_List2 Runat=server></asp:DataGrid> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid> ---> Hardik Joshi...

Add row in datagrid from other controls
Hello all, I am using the VS.2003.I would like to add a row in a datagrid, the data will come from other controls. I am having 2 textboxes (name, surname), 1 dropdownlist (sex) and a datagrid. I want to enter some values into these controls and when i am pressing a button (add) to insert them into datagrid row (the columns are also: name, surname and sex).The other issue is that i should add the ability of editing the values in the datagrid by pressing "Edit" button (it exists in each datagrid row). And the datagrid columns will have to be also 2 textboxes and 1 dropdownlist (jus...

Controls.Add in Composite Control that inherits another control
Hi! I created a composite control that inherits from the WebControls.Button. I added the following in the Load event:         Dim mHidden As New WebControls.HiddenField        Me.Controls.Add(mHidden) but the control is not created. Why?Dimitris PapadimitriouSoftware Development Professional Hi,Child controls on Button do not make much sense (how would you expect them to be rendered, doesn't button allow only text into value attribute?). Button allows child controls to be added, but they won't be rendered b...

add checkbox in Databound Schedule controls??
hi, i am using ScheduleCalendar from http://www.rekenwonder.com/. my question: how can i add checkbox in  the empty slots only?   thnx,   Hi betax, Actually you use a three part control which I have no right to make changes to it in this forum. I have looked at this free code control. It has <ItemTemplate>   <%# Eval("taskfieldname") %></ItemTemplate> just like data presentation controls in asp.net, so I guess you can generally add <asp:CheckBox> into <ItemTemplate> tag as what we do in GridView. It is just my o...

Should I use DataGrid(add dynamic control) or just dynamic control?
Hi, I am debating between using just dynamic control's or actually adding the control's dynamically into a datagrid.  I have tried with just adding dynamically, but I am having so much trouble because the selectedindexchanged even it firing for no reason.  I have asked a lot of people, but nobody knows the answer for that and I searched in the web and I am not getting anything either. Now someone suggested to use DataGrid and add the control's dynamically.  In each row, I have a dynamic column, which could be 2 and above.  It will depends on a number that I get fromt he ...

Add controls in textbox ( TextBox1.Controls.Add(); )
hi to alli want to know about TextBox1.Controls.Addit is possible to add controls in a textbox like placeholder i am using  this much of code for add linkbutton in a textbox 1 LinkButton lb = new LinkButton();2 lb.Text = "Linkbutton1";3 lb.ID = "link1";4 5 TextBox1.Controls.Add(lb);6 7 Response.Write (TextBox1.Controls.Count);8 Response.Write(TextBox1.Controls.ToString());this code run without any error when i count a control in textbox then it will disply me a 1 but on the page it will not display any controls in a textbox if this is...

How to add the data present in the controls to a table control without any database involvement.
Hi All,i am four controls where i will write the text and once i click the add button those should get added to the table .i want 6 columns fixed in the table and rows as many as required.i tried doing that taking a table server control and able to load the table.but the problem is whenver i am adding a new row the old one is gfetting overwritten by this and at the end i am getting only one row.  System.Web.UI.WebControls.Table t1 = new System.Web.UI.WebControls.Table();  i have declared the below one global TableRow tr; TableCell tc1; TableCell tc2; TableCell tc...

How to add new BLANK row in DataList control
Hi Friends, I am using DataList control to display items related to months in my Project using ASP.Net 1.1. There is a situation in which I have to add new blank row at the begining or end of every month. Say, for example, it's like a header for a particular month. Display a row with "January" as the text and below that row need to display items from 1st Jan to 31st Jan. This will repeat for each and every month. My Source code is as follow:HTML CODE:  1 <asp:datalist id="DataList1" style="LEFT: 0px; TOP: 32px" runat="server" Height=...

Data presentation control in another Data presentation control
 Hello,I want to make a menu with datalist or gridview control.Basically,the menu will be like this,Categories     Sub Categories.For example, for COMPUTING category there will be NOTEBOOKS DESKTOPS etc.There will be a image for CATEGORIES and linkbuttons with Subcategories.If I put a datalist inside a datalist and a link button inside the second datalist visually it satisfies my need.However,CommandArguments come from database to linkbutton.Andwhen one of these command buttons is cliked it must go to proper page.I dont know how to wrie an event_handler ,specifically, for...

Add controls to a server control
Hi,I have something like this<MyControls:SomeControl Id... runat...>    <MyControls:OtherControl Id... runat.../>    <MyControls:OtherControl Id... runat.../>    <MyControls:OtherControl Id... runat.../>...</MyControls:SomeControl Id... runat...>My class SomeControl has a MyOtherControlList ( as List<OtherControlType>) and a propertypublic OtherControlType OtherControl {    set { MyOtherControlList.Add(value); }}I never get into the property set() method. What am I doing wrong? What do I need to d...

How to add controls to a Control Collection?
Hello,I have an array of controls:Dim MyControls() As Control...How can I add all the controls in MyControls array to a control collection?Thanks,Miguel At it's simplest,. couldn't it look simply something like  For Each c As Control In MyControls Controls.Add(c) Next Thanks,Teemu KeiskiFinland, EU...

Web resources about - Cannot Add a blank Row to databound datagrid with Checkbox control - asp.net.presentation-controls

Tigraine
Daniel Hoelbling talks about programming Home Articles About me Articles Over the years I accumulated a number of (at least sometimes) useful ...

BlueVision, LLC - .NET Tips and Tricks
Note: Some tips might become outdated as new versions of the associated products are released. Debugging the OnStart method of a Windows Service ...

WPF « Dr. WPF
I’ve seen a number of forum posts lately expressing a desire for an observable dictionary. Here are links to a couple of them: Thread 1: Observable ...

onafterupdate event (Windows)
This event only fires when the object is databound and an HTMLImgEvents2::onbeforeupdate event has fired (for example, because the data has changed). ...

ASP.NET Grid Events
ASP.NET Grid Events - This example demonstrates the events available into the grid. The gird has following events: 'command', 'dataBound', 'selectionChanged', ...

ClientDataSource control for ASP.NET AJAX
The RadClientDataSource control allows populating all Telerik ASP.NET AJAX controls on the client side. Perform CRUD operations directly from ...

Coded Smorgasbord: Sweet Mysteries of Life
... And finally, Andrew sends us this example of defensive programming, that’s about as safe as we can make it: Private Sub ImageList_DataBound(ByVal ...

How To Skin Flotzam
Here’s how to skin Flotzam : 1. Download the latest Flotzam source code and open Flotzam.sln in Expression Blend. 2. Hit F5 to see the application ...

Declaring your DataContext in markup
... = new BookstoreViewModel(); } The result is a sparse design UI, on both MainPage.xaml and in the Create Data Binding dialog. I have databound ...

Listbox, Why Art Thou Blanking?
... 1. Using ValueConverters in your template ValueConverters are great – they allow you to transform your data on the fly as they are being DataBound ...

Resources last updated: 2/13/2016 1:43:46 PM