Solved

Unbound Dynamic datasource for datagrid control

Posted on 2004-09-09
23
3,411 Views
Last Modified: 2012-06-21
I want to create unbound datagrid control in a windows form. and want to assign dynamic datasource at runtime.

I want 2 columns in the datagrid, at the time of form load, datagrid should assign 2 columns and uncertain no. of rows having first column values and second blank column.

At the time of data entry, user will enter row values for second column.

Again user can add uncertain no of rows in that datagrid at the time of entry, this will be done on a button click event.

and user can add rows in between also, means user want to add rows between 2nd and 3rd  rows.

what will be the best way to do this? what type of datasource I can use to solve this problem.

I'll explain the problem with an exapmle -

for example at the time of form opens,  our datagrid looks like

Code    Values
a
b
c
d
Add Row Button

when user enter second column values datagrid looks like -
Code    Values
a           absolute
b           bicycle
c           cycle
d           danger
Add Rows Button        Add in between Row Button

again user has clicked the "Add row button" datagrid looks like the following -
Code    Values
a           absolute
b           bicycle
c           cycle
d           danger
a           absolute1
b           bicycle1
c           cycle1
d           danger1
Add Rows Button        Add in between Row Button

again when user clicks on "Add in between Row Button" with cursor pointing on b bicycle

then datagrid should look like

Code    Values
a           absolute
b           bicycle
b
c           cycle
d           danger
a           absolute1
b           bicycle1
c           cycle1
d           danger1
Add Rows Button        Add in between Row Button

I think I have clearly explains my problem.

i there any way I can do this?

thanks
SS



0
Comment
Question by:ssonia27
  • 9
  • 7
  • 6
23 Comments
 
LVL 5

Expert Comment

by:LindzK
ID: 12015029
Bind the datagrid initally to a dataset containing your datasource

datagrid1.datasource = dataset1.tables(0)

if you need extra columns that dont exist from the source of the data, before binding add the extra columns

Dim nc As New DataColumn("NewColumnName")
                nc.DefaultValue = ""
                dataset1.Tables(0).Columns.Add(nc)

then bind it to the datagrid using the datagrid.datasource.

When the user clicks the button to add a new row

Dim newrow as datarow
newrow = dataset1.tables(0).newrow
newrow.Item("ColumnName") = ""

the "" could come from a text field textbox1.text  or so

then
dataset1.Rows.Add(newrow)
dataset1.acceptchanges

then rebind the dataset to the datagrid
datagrid1.datasource = dataset1


Note : if this is for a web datagrid, you then have to datagrid1.databind(), you dont have to do that on a web form.
0
 
LVL 5

Expert Comment

by:LindzK
ID: 12015038
Also if your dataset contains none user friendly column names, you can change them to a nicer version for displaying in the datagrid, before doing the bind by doing this :-

dataset1.Table(0).Columns("strLogin").ColumnName = "Login"
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12015083
Hi ssonia27,

You can do what you want if you use an arraylist as your datasource and a class, something like

Dim al As New ArrayList

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
al.Add(New mything("a", ""))
        al.Add(New mything("b", ""))
        al.Add(New mything("c", ""))
        al.Add(New mything("d", ""))
        DataGrid1.DataSource = al
end sub

Private Sub AddNewRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addnewrow.Click
        al.Add(New mything("e", ""))
        DataGrid1.DataSource = Nothing
        DataGrid1.DataSource = al
    End Sub

    Private Sub AddBetween_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddBetween.Click
        al.Insert(DataGrid1.CurrentRowIndex + 1, New mything(DataGrid1.Item(DataGrid1.CurrentRowIndex, 1), ""))
        DataGrid1.DataSource = Nothing
        DataGrid1.DataSource = al
    End Sub

Class mything
    Private _Code As String
    Private _Value As String
    Sub New(ByVal SCode As String, ByVal SValue As String)
        Code = SCode
        Value = SValue
    End Sub
    Public Property Code() As String
        Get
            Return _Code
        End Get
        Set(ByVal Value As String)
            _Code = Value
        End Set
    End Property
    Public Property Value() As String
        Get
            Return _Value
        End Get
        Set(ByVal Value As String)
            _Value = Value
        End Set
    End Property

End Class
0
 
LVL 5

Assisted Solution

by:LindzK
LindzK earned 300 total points
ID: 12015162
To confirm my solution, here is some code that you place in the procedures you need ( form load / button click etc ) to get it all to work, the code includes finding the current position in the datagrid, to determine where to add a row inbetween of.

       'define a dataset
        Dim DS As DataSet
        Dim tbl As DataTable
        'create the columns needed
        Dim c1 As New DataColumn("Column1")
        Dim c2 As New DataColumn("Column2")
        'add the columns to the table
        tbl.Columns.Add(c1)
        tbl.Columns.Add(c2)
        'add the table to the dataset
        DS.Tables.Add(tbl)

        'creating datagrid here, just for example purposes
        Dim dg As DataGrid
        dg.DataSource = DS.Tables(0)

        '( this will b called from different procedure )
        'but placed here, for example purposes
        'when user clicks button to add row
        'first get the values you need from your form
        Dim columntext As String = "Users text taken from text box"
        Dim column2text As String = "Users other text taken from text box"

        'create a new row as a copy of the structure from the current dataset
        Dim newrow As DataRow = DS.Tables(0).NewRow
        newrow.Item("c1") = columntext
        newrow.Item("c2") = column2text
        DS.Tables(0).Rows.Add(newrow)
        DS.AcceptChanges()
        'rebind the dataset
        dg.DataSource = DS.Tables(0)

        '(this text is for the add inbetween function
        'get the current row position of the datagrid, so that
        'you know where to add it
        Dim rowposition As Integer = dg.CurrentRowIndex

        Dim newrow2 As DataRow = DS.Tables(0).NewRow
        newrow2.Item("c1") = columntext
        newrow2.Item("c2") = column2text
        DS.Tables(0).Rows.InsertAt(newrow2, rowposition)
        DS.AcceptChanges()
        'rebind the dataset
        dg.DataSource = DS.Tables(0)
0
 
LVL 5

Expert Comment

by:LindzK
ID: 12015180
The beauty of using a dataset, is that it  functions somewhat similar to transqactional databases, in that, if the user decides that they dont like the row they added, you can do roll back using

dataset.RejectChanges

So would alow you more control over what goes where - and whether or not it should be there :)

You can also then use the dataset.WriteXml if you want to ship the data stored in it, to other sources, which is quite nice too
0
 

Author Comment

by:ssonia27
ID: 12015328
thanks all for your quick reply let me check out with the solutions and then I'll give reply.
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12015481
Bye the way the xml part Lindzk talks about, is also posible for the solution I gave, since the class is serializeable
0
 

Author Comment

by:ssonia27
ID: 12063397
I have implemented both ways for my problem.
Both are excellent solutions.
I want to follow solution with dataset as a datasource for grid.
but I found one problem that at the time of form load I have added 2 rows in the grid.

problem is at run grid rows can be added only by click of buttons.

by using arraylist it is possible, grid is not allowing user to add any row.
but with dataset, we can add rows in the grid without clicking on buttons.

how can I prevent users to add row in the grid with dataset as a datasource,without any button click.
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12063424
instead of using a dataset, datatable use a dataview this way you can set

dv.allownew = false
dv.allowedit = true
dv.allowdelete = false
0
 
LVL 5

Expert Comment

by:LindzK
ID: 12063698
I'm presuming you have a button separate from the drig, that the user presses when they want to add a row to the dataset ?

If you use the code in the button, that I gave before - ie

 Dim columntext As String = "Users text taken from text box"
        Dim column2text As String = "Users other text taken from text box"

        'create a new row as a copy of the structure from the current dataset
        Dim newrow As DataRow = DS.Tables(0).NewRow
        newrow.Item("c1") = columntext
        newrow.Item("c2") = column2text
        DS.Tables(0).Rows.Add(newrow)
        DS.AcceptChanges()
        'rebind the dataset
        dg.DataSource = DS.Tables(0)

rather than off a button in the drid, are you sure it doesn't add the row ?  btw, if it is a vb.net web form you are using, you ahve to set the datasource of the grid, and then databind it as well.  As long as you rebind the dataset to the datagrid, after accepting the changes, you should be able to see the new row.

Can you post your existing code, so that I can have a look at it ?
0
 
LVL 5

Expert Comment

by:LindzK
ID: 12063708
Also try doing the accept changes from the table, rather than the full dataset, that may work too
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 5

Expert Comment

by:LindzK
ID: 12065071
Hi again :)

OK, here is step by step, what you do to create the datagrid ( assuming, you want one button to add rows ) and not a per row basis.

I have tested this, and it does add the rows, and visibly displays them inside your datagrid.

First off, I dragged and dropped a  datagrid onto the windows form

In the datagrid properties window, I set ReadOnly to true - to stop them typing into the existing rows.

Underneath the datagrid, in form designer - I added a button, with the text 'add new row' and one for 'add row inbetween current selection'

I also add two textboxes, to the form, for the user to enter in the
row information they need

Now, for the real code...

I create the dataset, as a global variable, as it needs to be accessed
by more than one function

In the form load section, I create the structure for my dataset ( please see coding example below. ) and bind it to the datagrid.

The rest of the example, can be seen via the code posted below :-
The main areas of the code, that you want to look at are the form load function, and the addrow function - I have posted the full class however, so that you can compare datagrid settings if necessary - to see where the problem is with the code you currently are using.


Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
    Friend WithEvents AddNew As System.Windows.Forms.Button
    Friend WithEvents AddInBetween As System.Windows.Forms.Button
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.DataGrid1 = New System.Windows.Forms.DataGrid()
        Me.AddNew = New System.Windows.Forms.Button()
        Me.AddInBetween = New System.Windows.Forms.Button()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.TextBox2 = New System.Windows.Forms.TextBox()
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'DataGrid1
        '
        Me.DataGrid1.DataMember = ""
        Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid1.Location = New System.Drawing.Point(24, 24)
        Me.DataGrid1.Name = "DataGrid1"
        Me.DataGrid1.ReadOnly = True
        Me.DataGrid1.Size = New System.Drawing.Size(416, 192)
        Me.DataGrid1.TabIndex = 0
        '
        'AddNew
        '
        Me.AddNew.Location = New System.Drawing.Point(336, 224)
        Me.AddNew.Name = "AddNew"
        Me.AddNew.Size = New System.Drawing.Size(96, 23)
        Me.AddNew.TabIndex = 1
        Me.AddNew.Text = "Add New Row"
        '
        'AddInBetween
        '
        Me.AddInBetween.Location = New System.Drawing.Point(168, 224)
        Me.AddInBetween.Name = "AddInBetween"
        Me.AddInBetween.Size = New System.Drawing.Size(152, 23)
        Me.AddInBetween.TabIndex = 2
        Me.AddInBetween.Text = "Add Row InBetween"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(24, 296)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.TabIndex = 3
        Me.TextBox1.Text = "TextBox1"
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(136, 296)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.TabIndex = 4
        Me.TextBox2.Text = "TextBox2"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(496, 334)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox2, Me.TextBox1, Me.AddInBetween, Me.AddNew, Me.DataGrid1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private DS As New DataSet()

    ' Initialise everything in the form load area
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim tbl As New DataTable()
        ' create the columns needed
        Dim c1 As New DataColumn("Column1")
        Dim c2 As New DataColumn("Column2")
        ' add the columns to the table
        tbl.Columns.Add(c1)
        tbl.Columns.Add(c2)
        ' add the table to the dataset
        DS.Tables.Add(tbl)
        ' bind the datagrid to the dataset
        DataGrid1.DataSource = DS.Tables(0)

    End Sub

    Private Sub AddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddNew.Click
        AddRow(0)
    End Sub

    ' get the row position, before adding the row
    Private Sub AddInBetween_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddInBetween.Click
        Dim rowposition As Integer = DataGrid1.CurrentRowIndex
        AddRow(rowposition)
    End Sub

    ' This function will add the new row, to the dataset
    ' parameters :
    '   index   -   index position to add the row to
    '               0 should be passed in, if you want to
    '               simply add a new row, without specifying
    '               the index for it.
    Private Sub AddRow(ByVal index As Integer)

        ' Get the column values from the text boxes
        Dim strColumn1 As String = TextBox1.Text
        Dim strColumn2 As String = TextBox2.Text

        'create a new row as a copy of the structure from the current dataset
        Dim newrow As DataRow = DS.Tables(0).NewRow
        newrow.Item("Column1") = strColumn1
        newrow.Item("Column2") = strColumn2

        ' determine where to add the row
        If Not index > 0 Then
            DS.Tables(0).Rows.Add(newrow)
        Else
            DS.Tables(0).Rows.InsertAt(newrow, index)
        End If

        DS.AcceptChanges()
        'rebind the dataset
        DataGrid1.DataSource = DS.Tables(0)

    End Sub

End Class



0
 
LVL 5

Expert Comment

by:LindzK
ID: 12065083
Hmm, two posts up, the word drig was meant to spell grid!  sorry for any confusion )
0
 

Author Comment

by:ssonia27
ID: 12071935
your solution is good but my requirement is to enter values in grid only, not from textboxex.

So I have to use that method only that will enable me to add rows in grid and user can type values in datagrid rows.
0
 
LVL 5

Expert Comment

by:LindzK
ID: 12072258
Can you then explain how you want it to work ? )

you want them to type in the grid then hit the button outside of the grid to add it ?

or do you want them to hit the button - grid becomes writeable, and place there text in - and it saves it?

or do you want them to hit  button to make grid writeable, but not store it in the dataset, till they hit button to save ?

If I understad the full process, I can try to help you come to a solution for it :)
0
 

Author Comment

by:ssonia27
ID: 12072365
I want that grid will always be writeable and user can add rows only by click on buttons. and currently by using dataset as a datasource when I'm typing values in the existing rows grid is automatically adding one new row in it. so I want to disable that.
and whatever user will type in the grid, at the same time that will also be stored in the dataset.
buttons job is to just add new rows in the grid. and then user can type in the rows.



0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 200 total points
ID: 12072417
Than I'll restate my previous comment use the dataview of your table

dim dv as new dataview = ds.tables(0).defaultview
dv.allownew = false
yourdatagrid.datasource = dv


than in your add button

dv.allownew = true
'add your record here
dv.allownew = false

0
 

Author Comment

by:ssonia27
ID: 12073043
I used LindzK's solution with RonaldBiemans's idea of dataview.
I want to split points between RonaldBiemans (300)and Lindzk(200)

SS
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12073055
Then you have to post a question in community support and ask if they can do that for you

http://www.experts-exchange.com/Community_Support/
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12073081
I can ask it for you if you want
0
 

Author Comment

by:ssonia27
ID: 12073143
oh yes
please ask the question.
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12074023
Hi ssonia27,

The question is reopened, now you can choose split points, and than you can assign the points as you want
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now