|
-
Apr 22nd, 2005, 07:45 AM
#1
Thread Starter
Junior Member
Change "Null" to empty " " help needed
Hi guys,i have another new problem.Using the opennetcf CSV adaptor,i managed to load a CSV into the datatable on the dataset.However,when i open the datagrid,some of the cells displays (null) where there is suppose to be empty.This affects my application as i need the cells to be empty.Anyone have any idea how to clear up the (null)?
Last edited by Irkiere; Apr 25th, 2005 at 08:07 PM.
-
Apr 22nd, 2005, 08:14 AM
#2
Thread Starter
Junior Member
Re: How to remove (null)?
Set the NullText property of the affected datagrid columns to any thing you want it to display to the user
I saw this statement in another thread.how do i go about doing it?
Sorry,i think i didn't make myself clear enough.To put it simply,i need my csv file display as it is.i want to remove all (null)that appears in the datagrid.
Last edited by Irkiere; Apr 22nd, 2005 at 08:32 AM.
-
Apr 23rd, 2005, 11:39 AM
#3
Fanatic Member
Re: Change "Null" to empty " " help needed
use this instead of the csvadapter class....works perfect for me
VB Code:
Public Function ReadCSV(ByVal p_fromCSVfile As String, ByVal p_toTableName As String, ByVal p_clearTable As Boolean)
If File.Exists(p_fromCSVfile) Then
'CSV variables
Dim l_sReader As New StreamReader(p_fromCSVfile)
Dim l_csvLine, l_csvArray() As String
Dim i As Integer
'Database variables
Dim l_sqlstmt As String
Dim l_sqlcmd As SqlCeCommand
Try
If p_clearTable = True Then
l_sqlstmt = "DELETE FROM " & p_toTableName
l_sqlcmd = New SqlCeCommand(l_sqlstmt, databaseConn.getInstance().GetConnection())
l_sqlcmd.ExecuteNonQuery()
End If
l_csvLine = l_sReader.ReadLine
'Inserts each line in the csv file into the table
While Not l_csvLine Is Nothing
l_csvArray = Split(l_csvLine, ",", -1)
l_sqlstmt = "INSERT INTO " & p_toTableName & " VALUES ("
For i = 0 To UBound(l_csvArray)
If i = UBound(l_csvArray) Then
l_sqlstmt = l_sqlstmt & "'" & l_csvArray(i) & "'"
Else
l_sqlstmt = l_sqlstmt & "'" & l_csvArray(i) & "',"
End If
Next i
l_sqlstmt = l_sqlstmt & ")"
l_sqlcmd = New SqlCeCommand(l_sqlstmt, databaseConn.getInstance().GetConnection())
l_sqlcmd.CommandText = l_sqlstmt
l_sqlcmd.ExecuteNonQuery()
l_csvArray = Nothing
l_csvLine = l_sReader.ReadLine
End While
Catch err As SqlCeException
MsgBox(err.Message, MsgBoxStyle.Information, "")
Catch err As IOException
MsgBox(err.Message, MsgBoxStyle.Information, "")
Finally
l_sReader.Close()
End Try
Else
MsgBox("File Does Not Exist", MsgBoxStyle.Information, "")
End If
End Function
Last edited by Strider; Apr 25th, 2005 at 11:05 AM.
Barry
Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
.NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0
SQL Server 2005/2000/SQL Server CE 2.0
If you like, rate this post
Compact Framework for Beginners
-
Apr 24th, 2005, 09:19 AM
#4
Thread Starter
Junior Member
Re: Change "Null" to empty " " help needed
Sorry strider,i'm already too far into my program to make the change that u suggested.I have no choice but to stick with the csv adaptor.can u offer me any other suggestion as to avoid having the "null" appear in my datagrid?Here is what happens when my form loads.....
Private Sub Form1_Load(By Val sender as Object, ByVal e As System.EventsArgs)Handles MyBase.Load
'load data
ds = New Dataset("MechanismLifeTest")
'get path to directory
appath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase )
'initialise adaptor
da = New CSVDataAdaptor(appath + "\MechanismLifeTest.csv", True)
'fill dataset from csv
da.Fill(ds, "MechanismLifeTest")
'display in grid
DataGrid.DataSource = ds.Tables("MechanismLifeTest")
End Sub
can u help me to add in the code that will stop "null" from appearing in my grid?Pls help in anyway if u can.Thanks!!
Last edited by Irkiere; Apr 24th, 2005 at 09:34 AM.
-
Apr 25th, 2005, 11:02 AM
#5
Thread Starter
Junior Member
Re: Change "Null" to empty " " help needed
I have solved this issue with some help.Will post asap when free to share with you guys.
-
Apr 25th, 2005, 08:06 PM
#6
Thread Starter
Junior Member
Re: Change "Null" to empty " " help needed
Private Sub SetNullEmpty(ByRef dt As DataTable, ByRef dg As DataGrid)
Dim r As Integer
Dim c As Integer
For r = 0 To dt.Rows.Count - 1
For c = 0 To dt.Columns.Count - 1
With dt.Rows(r)
.Item(c) = NZStr(.Item(c))
End With
Next
Next
dg.DataSource = dt
End Sub
Private Sub EditRecord_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
'load data
ds = New DataSet("MechanismLifeTest")
Dim apppath As String
'get path to application
apppath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase )
'initialise adapter
da = New CSVDataAdapter(apppath + "\MechanismLifeTest.csv", True)
'fill dataset from csv
da.Fill(ds, "MechanismLifeTest")
'display in grid
'DataGrid1.DataSource = ds.Tables("MechanismLifeTest")
Call SetNullEmpty(ds.Tables("MechanismLifeTest"), DataGrid1) End Sub
-
Apr 27th, 2005, 09:43 PM
#7
Lively Member
Re: Change "Null" to empty " " help needed
I see you already have a solution, so you may not care, but in case you or anyone else does, the way to do it without a function that converts the null is to set the NullText property of the column. You mentioned it in your first post. To set this property for your columns in the form designer you need yo map the table and its columsn to the datagrid and set the columsn nulltext property to "". Go to the datagrid's properties and click the "TableStyles" property collection (Keep in mind that by adding mapping this way in the designer you have to know the names of the tables/columns beforehand, all of this can also be done programatically). In the tablestyles dialog you can add each table your dataset will hold. The mapped name for a default, unnamed table in a dataset is "Table" I beleive. Add the tables with the mapped names you need, then click the tables ColumnStyles collection property. Here you can map the columns in the dataset to the grid. Make sure the mapped names match the column names in the dataset's table or they will not display on the grid. Each column also has a nulltext property. Clear this property, which by default has "(null)" in it. You now have columns ready to hold your data that will display empty string where there are null values.
I'm not 100% I wrote this all out perfect so feel free to correct any mistakes I have guys. I'm friggin tired and i'm going to sleep. night.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|