Blank Rows in DataTables

I've implemented a function to insert a blank row into my datatable, however, I'm running into a few issues with it. Namely, it's not adding a blank entry for all the columns. Is there a way to iterate through the columns in my combobox, and based on the datatype of the column, insert the appropriate blank value?

Currently I have the following:

FriendSharedFunction InsertBlankRow(ByVal SourceAs DataTable)As DataTable

Dim myNewSourceAs DataTable = Source

Dim myBlankRowAs DataRow = myNewSource.NewRow

Dim iAsInteger

MessageBox.Show(myNewSource.Columns.Count)

For i = 0To myNewSource.Columns.Count - 1

MessageBox.Show("DataTypeis: " & myNewSource.Columns(i).ToString)

'If (myNewSource.Columns(i).DataType() = Odbc.OdbcType.DateTime) Then

' MessageBox.Show("Datatype is datetime!")

'End If

myBlankRow(i) = ""

Next

myNewSource.Rows.InsertAt(myBlankRow, 0)

Return myNewSource

EndFunction

The problem with this is that it will error out if I try and pass it a datatable that has a datetime column in it. How can I account for that? (You can see that I've tried to check for this and failed. :S)

[3364 byte] By [Kaleb] at [2007-12-16]
# 1

I'm not sure what you mean by "blank" - you can't have a "blank" DateTime or a "blank" integer value. You can specify default values for these - or, if your data model supports it, they can be set to DBNull.Value.

My first suggestion would be to set the default "blank" values for each column in the DataSet (either in the designer or at runtime). When you call NewRow, the default value will be automatically set for each column. For example, at runtime you can do the following to set the default value of a DateTime column to the current DateTime (example is with NorthwindDataSet using the Orders Table):


Me.NorthwindDataSet1.Orders.OrderDateColumn.DefaultValue = DateTime.Now

Any new Orders row will have a default value of the current Date for the OrdersDate column.

Joe Stegman
The Windows Forms Team
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights.

JoeStegman at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2
By blank, I'm trying to manipulate the dataset so that the first row of the combo box is equivalent to when setting the selectedindex to -1 (twice) is working as expected for me. For some reason, I can't seem to make that work with a dataset.

A problem with setting default values is that in some cases, we're dealing with autonumber which can't have a default value. Another problme is we don't know at runtime which dataset is being passed in, so I'd like a method of checking the datatype, and if it matches a specific datatype, put in an appropriate value for that column.
Hopefully this makes a little more sense.
Thanks again for all your assistance, Joe. You rock! :D

Kaleb at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3

I'm still not positive I understand the scenario. I think it's the following:

You have a DataTable in a DataSet
You are binding it to a ComboBox
You want to add a "blank" first row to the DataTable
You may set the ComboBox DisplayMember to a column that is a DateTime
In this scenario, you want the ComboBox to show blank ("")
Note, this is only when the ComboBox is bound to the "blank" row

Is this correct? If so, then it will require the following:

You need to add your blank row (as you have done)
Check if the column type is a DateTime (as you have done)
If it is a DateTime, then set the value to DateTime.Min (rather than "")
The ComboBox will show blank for DateTime.Min
Joe Stegman
The Windows Forms Team
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights.

JoeStegman at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4
I agree I haven't done a very good job of explaining the subject, however you're close:

I have a DataTable from a DataSet (which is pulling 5 columns out of the database)

I am setting (for example) the display member to be the 4th column, and the Value member to the 1st column.

I can't always assume that the first column of my datatable will be the display member, and so I need to have a blank row that covers all of the columns. When I try and iterate through the columns to insert a blank value in each column, I get an error on int as well as datetime. Trying to catch when it's those datatypes has failed for me, as I can't seem to find the right syntax to do a type check.

I'm essentially trying to mimc the .SelectedIndex = -1, which isn't working for me at all.

(I apologize for my posting today...I've got a bit of sunstroke. :) Makes coding really interesting... :S)

Kaleb at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5

I'm still not positive I've got it right - so I'll take it one step at a time. To check the type, you need to do the following:


If myNewSource.Columns(i).DataType() Is GetType(DateTime)

Or possibly, what you need is:


If Not myNewSource.Columns(i).DataType() Is GetType(String)

Joe Stegman
The Windows Forms Team
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights.

JoeStegman at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 6
That helped me to determine what datatype I'm working with, thank you Joe.

I'm going to try and be more specific about what it is I'm trying to do:
I have a database of Servers and attributes associated with the servers. All of the database design was done prior to my coming to work on this project. I have a combo box which lists the Server Name and the Asset ID tag for it. I have those being populated from the same datatable so they are synchronized.

I have a 3rd combo box which lists the status (production, retirement, development, etc) to further filter the datatable.

Right now, I'm implementing class-wide datatable variables (I have 7 or 8 right now). I want the initial view of the form to show blank combo boxes. For some reason, if I apply a defaultview.rowfilter or a defaultview.sort feature to the datatable, adding a new row doesn't blank the initial view of the combobox.

I need to have the first row appear blank because if I don't, it looks really funny (because the remainder of the form are controls to display further details about the server).

Hopefully that makes some sense? Tongue Tied

Kaleb at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 7

Kaleb wrote:
if I apply a defaultview.rowfilter or a defaultview.sort feature to the datatable, adding a new row doesn't blank the initial view of the combobox.

I'm not sure what your Sort or RowFilter is doing - it is possible the RowFilter is removing the row you added (it doesn't match the filter) or the Sort is sorting your item to some place other than the first item.

Joe Stegman
The Windows Forms Team
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights.

JoeStegman at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 8
You beat me to it, Joe! I had an epiphany last night before I left work (which was before I saw your post, unfortunately) that in my attempt to make my data access somewhat efficient, I pulled one dataset and started filtering/sorting that! So of course my blank row wouldn't match the filtering criteria I applied. It looks like I'll either have to pull down a seperate dataset or I'll have to live with the combo-boxes as they are now.

If you can think of a better way, I would love to try and implement it! Otherwise, I think this case is solved. Sad

Kaleb at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...