Editing a data-grid at runtime!

Master Zero

Well-known member
Joined
Sep 10, 2005
Messages
51
Programming Experience
Beginner
Good afternoon!

I need some help editing a data-grid table by adding a new row (s) to it. I’ am currently using the code below to create the data-grid, but I’ am having trouble updating it. Every time the routine runs, it will erase the previous row, which is my problem.

VB.NET:
[FONT=Courier New] 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ListUsers()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] DataArray() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' Message parts are divided by "|" Break the string into an array accordingly.
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] DT [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataTable = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataTable
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] DR [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataRow
DT.Columns.Add([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataColumn("Nick Name", [/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])))
DT.Columns.Add([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataColumn("Host Name", [/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])))
DT.Columns.Add([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataColumn("IP Address", [/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])))
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] I [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] I = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] 0
DR = DT.NewRow()
DR(0) = Users(1)
DR(1) = Users(2)
DR(2) = Users(3)
DT.Rows.Add(DR)
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DataGrid1.DataSource = DT
[/SIZE][/FONT]

Basically, I need to know how to add a new row to an existing data-grid at runtime.

Thanks in advances.
 
Each time the procedure is called you are designating DT as a New DataTable.
This will wipe out the information from the previous run of the procedure.

Here's what I usually do:
'Define the Table
Dim DT As DataTable
'Set the DataTable to a dataset
DT = DataSet1.Tables("TableName")
'Define the data row
Dim DR As DataRow
DR = DT.NewRow()
'Set the Values
DR("FieldName") = someValue
'Add The Row
DT.Rows.Add(DR)

'Call the dataset Update
Try
Me.UpdateDataSet()
Catch eLoad As System.Exception
System.Windows.Forms.MessageBox.Show(eLoad.Message)
End Try
 
Thanks for the reply, but I already found another way.

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] I [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] DR [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataRow
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] DT [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataTable = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataTable
DT.Columns.Add([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataColumn("Nick Name", [/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])))
DT.Columns.Add([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataColumn("Host Name", [/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])))
DT.Columns.Add([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataColumn("IP Address", [/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])))
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] I = 1 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] Users.Length - 1
DR = DT.NewRow
DR(0) = Users(I)
DR(1) = Users(I + 1)
DR(2) = Users(I + 2)
DT.Rows.Add(DR)
I += 2
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DataGrid1.DataSource = DT
[/SIZE]

I just send all of the Info at one time, than I break it up into a data-array. Now it is just a matter of using the For Next loop to add them to the Data-grid.

I did try your code and I am getting an error at the last line "Me.UpdateDataSet()". I also tried "Me.DataGrid1.UpdateDataSet(DT)" but I am still getting an error.
 
SORRY FOR THE CONFUSION,
THE LINE ' Me.UpdateDataSet()' CALLS THE SUB ROUTINE TO UPDATE THE DATASET.
THAT CODE IS USED TO UPDATE THE DATA THROUGH A DATA ADAPTER BACK TO THE DATABASE. IT IS LIKE THE CODE GENERATED BY THE DATAFORM WIZARD FOR OLEDB DATA ADAPTERS.
WHERE ARE YOU SAVING THE DATA?

FYI:
YOU ARE INCREMENTING YOUR COUNTER INSIDE THE LOOP. THIS IS NOT THE INTENTION OF A 'FOR' LOOP. USE ANOTHER COUNTER TO GO FROM 0 TO 2.
Dim T As Integer = 0
For I = 0 To Users.Length - 1
DR = DT.NewRow
DR(T) = Users(I)
DT.Rows.Add(DR)
If T >= 2 Then
T = 0
Else
T += 1
End If

Next
Me.DataGrid1.DataSource = DT

YOU WILL NEVER GET DATA IN THE FIRST POSITION OF Users. IT IS ALSO ZERO BASED SO THE FIRST PIECE OF DATA SHOULD BE AT Users(0). YOUR CODE WILL BE MISSING ONE 'CELL' For I = 1 To Users.Length - 1. SAY THE LENGTH IS 50. DATA IS LOCATED IN 'CELLS' 0 TO 49. YOUR LOOP GOES FROM 1 TO 49.

I DON'T MEAN TO BE PICKING ON THE CODE...IT OBVIOUSLY WORKS, BUT YOU SHOULD LEARN GOOD HABITS/PRACTICES ALSO.
 
Last edited:
VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#008000]' Process the command received from the server, and take appropriate action.
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ProcessCommands([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] strMessage [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] DataArray() [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#008000]' Message parts are divided by "|" Break the string into an array accordingly.
[/COLOR][/SIZE][SIZE=2][COLOR=black]DataArray = strMessage.Split(Chr(124))[/COLOR]
 
[SIZE=2][COLOR=#008000]' dataArray(0) is the command.
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Select[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] [COLOR=black]DataArray(0)[/COLOR][/SIZE]
[SIZE=2][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] [COLOR=black]"JOIN"[/COLOR]
[/SIZE][SIZE=2][COLOR=#008000]' Server acknowledged login.
[/COLOR][/SIZE][SIZE=2][COLOR=black]DisplayText("You have joined the chat" & Chr(13) & Chr(10))[/COLOR]
[COLOR=black]Chat.ReadOnly =[/COLOR] [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] [COLOR=black]"CHAT"[/COLOR]
[/SIZE][SIZE=2][COLOR=#008000]' Received chat message, display it.
[/COLOR][/SIZE][SIZE=2][COLOR=black]DisplayText(DataArray(1) & Chr(13) & Chr(10))[/COLOR]
[/SIZE][SIZE=2][COLOR=black]Case[/COLOR][/SIZE][SIZE=2][COLOR=black] "REFUSE"[/COLOR]
[/SIZE][SIZE=2][COLOR=#008000]' Server refused login with this user name, try to log in with another.
[/COLOR][/SIZE][SIZE=2][COLOR=black]AttemptLogin()[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] [COLOR=black]"LISTUSERS"[/COLOR]
[/SIZE][SIZE=2][COLOR=#008000]' Server sent a list of users.
[/COLOR][/SIZE][SIZE=2][COLOR=black]Users = strMessage.Split(Chr(124))[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].[COLOR=black]Invoke(CallDataGridBinder)[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] [COLOR=black]"BROAD"[/COLOR]
[/SIZE][SIZE=2][COLOR=#008000]' Server sent a broadcast message
[/COLOR][/SIZE][SIZE=2][COLOR=black]DisplayText("ServerMessage: " & DataArray(1) & Chr(13) & Chr(10))[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] [COLOR=black]"FILEINFO"[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]Select[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] [COLOR=black]MessageBox.Show(DataArray(1) & _[/COLOR]
[COLOR=black]" wants to send you a file named (" & DataArray(2) & _[/COLOR]
[COLOR=black]"), which is (" & DataArray(3) & ") bytes in size." & vbNewLine _[/COLOR]
[COLOR=black]& "Do you wish to download (receive) the file?", [/COLOR][/SIZE][SIZE=2][COLOR=black]Me[/COLOR][/SIZE][SIZE=2][COLOR=black].Text, _[/COLOR]
[COLOR=black]MessageBoxButtons.YesNo, MessageBoxIcon.Information)[/COLOR]
[/SIZE][SIZE=2][COLOR=blue]Case[/COLOR][/SIZE][SIZE=2][COLOR=black] DialogResult.No[/COLOR]
[COLOR=black]SendData("REPLY|" & DataArray(1) & "|" & "NO")[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] [COLOR=black]DialogResult.Yes[/COLOR]
[COLOR=black]strFileName = DataArray(2)[/COLOR]
[COLOR=black]intFileSize = DataArray(3)[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] [COLOR=black]Directory.Exists(Application.StartupPath & "\Downloads\") _[/COLOR]
[COLOR=black]= [/COLOR][/SIZE][SIZE=2][COLOR=black]False [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=black]Directory.CreateDirectory(Application.StartupPath & _[/COLOR]
[COLOR=black]"\Downloads\")[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'Do Nothing
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=black]Path.Text = Application.StartupPath & "\Downloads\" & DataArray(2)[/COLOR]
[COLOR=black]SendData("REPLY|" & DataArray(1) & "|" & "YES")[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Select
[/COLOR][/SIZE][SIZE=2][COLOR=blue]Case[/COLOR][/SIZE][SIZE=2][COLOR=black] "READY"[/COLOR]
[/SIZE][SIZE=2][COLOR=#008000][COLOR=black]ReceiveFile()[/COLOR]
[/COLOR][/SIZE][SIZE=2][COLOR=blue]Case[/COLOR][/SIZE][SIZE=2][COLOR=black]"REPLY"[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] [COLOR=black]DataArray(1) = "NO"[/COLOR] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=black]MessageBox.Show(DataArray(2) & " has refuse the file trasnfer" & _[/COLOR]
[COLOR=black]" request!", [/COLOR][/SIZE][SIZE=2][COLOR=black]Me[/COLOR][/SIZE][SIZE=2][COLOR=black].Text, MessageBoxButtons.OK, _[/COLOR]
[COLOR=black]MessageBoxIcon.Information)[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] [COLOR=black]DataArray(1) = "YES"[/COLOR] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=black]MessageBox.Show(DataArray(2) & " has accepted the file trasnfer" & _[/COLOR]
[COLOR=black]" request!", [/COLOR][/SIZE][SIZE=2][COLOR=black]Me[/COLOR][/SIZE][SIZE=2][COLOR=black].Text, MessageBoxButtons.OK, _[/COLOR]
[COLOR=black]MessageBoxIcon.Information)[/COLOR]
[/SIZE][SIZE=2][COLOR=#008000][COLOR=black]SendData("READY|")[/COLOR]
[/COLOR][/SIZE][SIZE=2][COLOR=black]SendFile()[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'Do Nothing
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Select
[/COLOR][/SIZE][/SIZE][/SIZE][/SIZE][/COLOR][/SIZE]


As you can clearly see, if I had started from 0 to Users.Length - 1, then the command "LISTUSERS" would also have been added to the Datagrid. That is the reason why I did not start from Zero and did not get an error.

DavidT_macktool said:
WHERE ARE YOU SAVING THE DATA?
The list is not being saved; it is just showing currently online users in a Datagrid.
 
Last edited:
Back
Top