1
votes

Simple question!

I have DataSet filled with three DataTables. Somehow, I need to hide some columns in GridView. Now there are two ways to hide columns:

  1. Hide columns from DataTable using DataColumn MappingType property.

myDataSet.Tables("Managers").Columns("Id").ColumnMapping = MappingType.Hidden

  1. Hide column directly from GridView

    GridView1.Columns("Id").Visible = False

OR

    GridView1.Columns("Id").VisibleIndex = -1

Now keeping the columns hidden, there is a functionality that I need to provide is to "Add new Row". While the column is hidden, how I can add data in my new row?

More precisely, how I can put data in hidden column cells?

Suppose that my Id column is not Auto-Incremented.

NOTE: I am working in VB.NET Desktop environment.

1
who is going to add data in hidden fields, you or the end user? - Sarvesh Mishra
You're able to add data to hidden fields just as you would if they were visible (programmatically). If you are confused about how the user would type it in if it's hidden, then I would suggest adding a textbox and button beside the datagridview that puts the information into the hidden column of the selected row, or something to that effect - AaronTheRabbit

1 Answers

0
votes

My assumption is that you are using a bindingsource with the datagridview? Add the dataset to the datasource for your project (Menu: Data->Add New Data Source). Then Menu: Data->Show Data Sources. Right click on the table and select to show it as a datagrid. Drag the table onto your form. Right click on the grid to Edit Columns. Delete the columns you don't want, format the rest. The designer will automatically create a bindingsource for you. In your form code, select the Bindsource on the top left pulldown list and then 'AddingNew' on the top right list. A new event will be created for you. Inside the event, add the following code:

   Private Sub YourTableBindingSource_AddingNew(ByVal sender As Object, _
                                              ByVal e As system.ComponentModel.AddingNewEventArgs) _
         Handles bsyourBinding.AddingNew

        Dim bs As BindingSource = CType(sender, BindingSource)
        Dim view As System.Data.DataView = CType(bs.List, System.Data.DataView)
        Dim _row As System.Data.DataRowView

        _row = view.AddNew
        dim newRow as yourDataSet.yourTableRow = DirectCast(_row.Row, yourDataSet.yourTableRow)
        With newRow 
            .BeginEdit()
            NextIDNumber += 1
            .MyIDField = NextIDNumber
            .MyDataField1 = Today.AddDays(1).Date
            .MyDataField2 = someData
            .MyDataField3 = someOtherData
            .EndEdit()
        End With
        bs.MoveLast()
        e.NewObject = _row
    End Sub

This binding source AddingNew event is particularly useful when you are adding records that belong to a parent table. As the tableis bound to the grid using a bindingsource, all changes made to the bindingsource are automatically reflected in the grid. You don't have to display the ID Number in the grid. If a user clicks on a new line on the grid, this binding source event will be automatically fired.

I would also include some code for the grid events CellParsing, CellValidating, CellValidated (use this to clear out error messages and maybe do some math) and RowValidating. In Row Validating you can check that all of the cells have values and are valid. If the user has left a blank line, you can call .CancelEdit to delete the blank line and prevent it from getting stored to the database.