Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 544
  • Last Modified:

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
0
SGUDAPAT
Asked:
SGUDAPAT
  • 4
  • 3
1 Solution
 
carlsiyCommented:
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.
0
 
SGUDAPATAuthor Commented:
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.
0
 
carlsiyCommented:
for
#1
you can use e.g. "Select field1, field2, 0 as field3 from sample_table " 
the return here would be
field1     field2     field3    
<data>  <data>    0
<data>  <data>    0
<data>  <data>    0

#2
heres a quick sample (creation of data table and its columns)

  Dim myDataTable As DataTable = New DataTable()

  Dim myDataColumn As DataColumn

  myDataColumn = New DataColumn()
  myDataColumn.DataType = Type.GetType("System.String")
  myDataColumn.ColumnName = "id"
  myDataTable.Columns.Add(  myDataColumn)

  myDataColumn = New DataColumn()
  myDataColumn.DataType = Type.GetType("System.String")
  myDataColumn.ColumnName = "username"
  myDataTable.Columns.Add(  myDataColumn)

  myDataColumn = New DataColumn()
  myDataColumn.DataType = Type.GetType("System.String")
  myDataColumn.ColumnName = "firstname"
  myDataTable.Columns.Add(  myDataColumn)

  myDataColumn = New DataColumn()
  myDataColumn.DataType = Type.GetType("System.String")
  myDataColumn.ColumnName = "lastname"
  myDataTable.Columns.Add(  myDataColumn)


#4
yes there is its called the Timer Object.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
SGUDAPATAuthor Commented:
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

0
 
carlsiyCommented:
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.)


0
 
carlsiyCommented:
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.
0
 
SGUDAPATAuthor Commented:
Hi Carlsiy,
Thanks for the solution.
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now