Click here to Skip to main content
15,918,193 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Master...

how to keep the string z can be a function to add a row in the datagridview. ive tried various ways and eventually fail.

I want to make it function add dynamic row.

Thank you for the help...

sorry for my bad english

What I have tried:

Dim z As String ="dt.Rows(i)(""Nomor""), dt.Rows(i)(""Tanggal""), dt.Rows(i)(""Nama Tercetak""), dt.Rows(i)(""Qty""), dt.Rows(i)(""Harga""), dt.Rows(i)(""Jumlah""), dt.Rows(i)(""Paid Status"")"

For i = 0 To dt.Rows.Count - 1

DataGridView1.Rows.Add(z)

Next
Posted
Updated 22-Sep-16 16:59pm
Comments
Dave Kreskowiak 22-Sep-16 22:27pm    
That makes no sense at all. What are you trying to do with this code?

As you coded your example, Rows.Add() will try to add a string. It has no capability to execute code in a string.
Hendi Hermawan 22-Sep-16 22:34pm    
is there any way that the string can be executed in rows.add ()?

I want to string sql query result of which I keep in a string z can execute in rows.add ()

thnx for ur help Dave
Dave Kreskowiak 22-Sep-16 22:46pm    
No.

And what you're saying still doesn't make any sense at all.

1 solution

If I understand you correctly, why not add the data into a data table and bind that data table to the datagridview. For example
VB
Dim MyDataTable As DataTable
Dim MyDataColumn As DataColumn
Dim MyDataRow As DataRow

MyDataTable = New DataTable()
MyDataColumn = New DataColumn()
MyDataColumn.DataType = GetType(String)
MyDataColumn.ColumnName = "Name"
MyDataTable.Columns.Add(MyDataColumn)

MyDataRow = MyDataTable.NewRow
MyDataRow("Name") = "Nomor"
MyDataTable.Rows.Add(MyDataRow)

MyDataRow = MyDataTable.NewRow
MyDataRow("Name") = "Tanggal"
MyDataTable.Rows.Add(MyDataRow)

MyDataRow = MyDataTable.NewRow
MyDataRow("Name") = "Nama Tercetak"
MyDataTable.Rows.Add(MyDataRow)

Me.MyDataGridView.DataSource = MyDataTable

Now by adding new rows to the data table they are added to the datagridview.
 
Share this answer
 
v2
Comments
Hendi Hermawan 22-Sep-16 23:27pm    
thnx mika for ur answer but thats not my solution, this my full code, i hope u will understand with my code :)

Sub caridata()

dt.Clear()
Call koneksi()
Dim SqlQuery As String = "SELECT * FROM " & Label4.Text & " where Tanggal >= @dtawal and tanggal <= @dtakhir"
With cmd
.Parameters.AddWithValue("@dtawal", Format(DateTimePicker1.Value, "yyyy-MM-dd"))
.Parameters.AddWithValue("@dtakhir", Format(DateTimePicker2.Value, "yyyy-MM-dd"))
.CommandText = SqlQuery
.Connection = conn
End With
With adapter
.SelectCommand = cmd
.Fill(dt)
End With
Try
Call koneksi()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "select * from tbllaporan2 where [caption form]='" + Me.Text + "'"
Cari2 = cmd.ExecuteReader
If Cari2.HasRows = True Then
Cari2.Read()
Dim asalfield, arrow(), union As String

asalfield = Cari2("Asal Field")
arrow = asalfield.Split("|")
DataGridView1.Rows.Clear()
With DataGridView1
For a As Integer = 0 To arrow.Length - 2
union = union + " dt.Rows(i)(""" + arrow(a) + """),"

Next
MsgBox(Len(union))

End With
For i = 0 To dt.Rows.Count - 1
Dim t = Len(union) - 1
Dim z As String = Microsoft.VisualBasic.Left(union, t)

'MsgBox(z)
DataGridView1.Rows.Add(z)
'dt.Rows(i)("Nomor"), dt.Rows(i)("Tanggal"), dt.Rows(i)("Nama Tercetak"), dt.Rows(i)("Qty"), dt.Rows(i)("Harga"), dt.Rows(i)("Jumlah"), dt.Rows(i)("Paid Status")
Next

cmd.Parameters.Clear()

Else
Label4.Text = ""
End If
conn.Close()
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
Wendelius 22-Sep-16 23:34pm    
Correct me if I'm wrong but you seem to fetch the data into a single string variable and use a comma as delimiter between items.

If this the idea, it's exactly what is causing the trouble and I don't see any reason for such complicated approach. Instead of using a single string, fetch the data into a data table using DbDataAdapter.Fill Method (DataTable) (System.Data.Common)[^] and then use the data table as the data source as in the example.
Hendi Hermawan 22-Sep-16 23:42pm    
yes true mika, im fetch data into single string variable and use a comma as delimiter between items.

im still confused mika :)

if this can be solved, this maybe be great idea for create very dynamic datagridview. just 1 form and 1 datagrid will be create a lot of report :)
Wendelius 22-Sep-16 23:50pm    
As far as I can see you can:
- fetch the data you want in each situation
- let the datagridview create the columns or create them 'by hand'
- make all desired modifications to the data using the data table

This should keep the design very simple, but as I don't know the whole situation, you'll have to try if it fits your needs :)
Hendi Hermawan 23-Sep-16 1:28am    
ok, thnx for ur advice mika :) , ill try then... :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900