Link to home
Start Free TrialLog in
Avatar of SGUDAPAT
SGUDAPATFlag for United States of America

asked on

Can I Enter values into Data Grid View manually- Urgent Help

Hi,

I am developing a vb.net windows forms application.
I have a data grid view in this application, I am retreiving data into the data grid view using ado.net.
I added an extra column manually using code below:
Please help me the issues I have:
1.I am able to get the column in my data grid view, but it empty, I can understand that I haven't passed any data to the column so it is displaying empty(since this column is not from the database), what should I do inorder to set the default value in the column for all the rows as Zero '0'.
2.How can I restrict the user from entering characters, I want the user to enter only numeric values.
3.When I click a button all the values in all the rows of this column should change from '0'  to  '1'.
4.Is they any option to refresh the windows form for every 30 seconds.

I also have attached the data grid view ouput, where I have got the extra column without any data since it is not from the database.


If GetDataSet(DS, sSQL) Then
            With DS.Tables("Table")
                count = .Rows.Count
                If .Rows.Count > 0 Then
                    dt = DS.Tables("Table")
                    RDGVW.DataSource = dt
                    RDGVW.Columns.Add(num, "NUMBERS")
                End If
            End With
        End If

Open in new window

code.doc
Avatar of carlsiy
carlsiy
Flag of Philippines image

answers...
for
#1
 you can add the statement in your select query such as.. "select .... , 0 as columnname from  ....."  the returning query would have a column with 0 values.
#2
you have to manually make a datatable, normally datatable's are just passed from sql query output. but you can also modify the datatable structure and component, what you need here is to inherit the NumericUpDown object within a column of the datatable.
#3
This can be done through looping through all the row of the datatable to change the cell value of a specific column.
#4
Yes, there is put the refresh option within a timer object.
Avatar of SGUDAPAT

ASKER

Hi Carlsiy,

 #1:Do you mean to say that I should write the select query for the data table which is created manually?
#2:I am a beginner into VB.Net, could please tell me how I can create datatable manually.
#4:Does Vb.Net have any in-built timer objects which can be used or do I need to create a timer object.
ASKER CERTIFIED SOLUTION
Avatar of carlsiy
carlsiy
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Carlsiy,

I am little confused about the data table, in my application I created a data table like the code below and passed it to the data grid view, to populate the data grid view, and I have added an extra column manually, can you explain me once again what I should do inoder to set the default value of the extra column which which I manually created to '0'.
 Dim dt As New DataTable
        Dim DS As New DataSet
        'Select the data from the table and sets it to the dataset
        sSQL = "SELECT RECEIVER_ID AS RECEIVER_NO, RECV_LN_NBR AS      
                LINE_NO, INV_ITEM_ID AS ITEM_ID " & _
              "FROM PS_THI_RECV_PRINT WHERE PRINT_BAR_CODE = 'N' ORDER BY LINE_NO"
        If GetDataSet(DS, sSQL) Then
            With DS.Tables("Table")
                count = .Rows.Count
                If .Rows.Count > 0 Then
                    dt = DS.Tables("Table")
                    RecevierDGVW.DataSource = dt
                    With RecevierDGVW.Columns
                        .Add(numLbl, "NUM. OF LABELS")
                    End With
                End If
            End With
        End If
    End Sub

Open in new window

The stlye of datatable creation that you are using is automatic, which means that the columns and constraints of the datatable depends on the return of your query, based on the original datasource. so e.g. if receiver_no in your database is text the datacolumn created in the datatable is data typed string.

if you are to manually add it like this
  myDataColumn = New DataColumn()
  myDataColumn.DataType = Type.GetType("System.Double") 'Declared the datatype
  myDataColumn.ColumnName = "receiver_no"
  myDataTable.Columns.Add(  myDataColumn)

opposed to this
                    With RecevierDGVW.Columns
                        .Add(numLbl, "NUM. OF LABELS")
                    End With
which you just added a textbox column to your datagrid view

The more controlled flow would be... SQL Query --> DataTable or DataSet --> DatagridView

SQL Query => Defines the source of data
DataTable or DataSet => Defines the column length, DataType etc...
DataGridView => Defines the U.I. for the DataTable or DataSet output (normally its a textbox, but can be changed into a NumericUpDown object to fit your needs.)


But for your purpose...
     
  For i As Integer = 0 To RecevierDGVW.Rows.Count
            RecevierDGVW("<column_Name>", i).Value = 0
  Next

this should fix it... but only for the Datagridview not tha datatable.
Hi Carlsiy,
Thanks for the solution.