vs 2008 vb - How do I set a default value for a column at runtime for a bound datagridview

Locked vs 2008 vb - How do I set a default value for a column at runtime for a bound datagridview

  • Friday, March 02, 2012 4:11 PM
     
     

    I have a data bound datagridview. I want to be able to have column 3 default value is = 0,

    column 4 default value is = 1 and column 5 default value is = 0.  How can I do This.  I just

    dragged the table onto the form visual basics data source panel. I have 20 different colums

    some have 0's, some have 1's adn some ave 2's.  While entering the data, well it takes

    forever to enter 3000 of these 20 columns.  I think I would need 2o textbox's to place the

    value and a button to set the default value's fir the columns.  Usually 40 rows are the same

    for these 20 colums and 3 other columns are not related.  Or it would even work if I could

    maybe eneter 3000 row with 0's in all the 20 columns and leave the other 3 columns blank.

    but it would be better the first way.  Can any one help me out.  It's a bound datagridview.

    jere


    Jere Small

All Replies

  • Friday, March 02, 2012 4:39 PM
     
     

    Presuming you are using a typed dataset then double click the .xsd file to open the dataset designer.

    Highlight the column you want to set a default value on and set it in the properties window.

  • Friday, March 02, 2012 5:24 PM
     
     

    Jere,

    You can define default values :

    • in the datagridview. Click on your datagridview, click the small arrow on the right-hand side, edit the columns. Click on the required column and edit the property "DefaultCellStyle". In the "Null value" sub-property, you can enter anything you want. It will replace a null value for display.
    • in the database. If yu use SQL Server, you can define a Default Value for each column of your database

    If this isn't enough for you, you can also write your own "filling default values" procedure by simply iterating over the lines of your datagridview. Also, if your datagridview is filled from a class, you can define default values there.

  • Friday, March 02, 2012 8:12 PM
     
     

    thanks I think what I want to do, is fill in textbox's and then when I create a new row it would use the values in the textbox as the value for the cells. Like, maybe in

    DataGridView_CellEndEdit     or

    DataGridView_RowsAdded

    This is a weird notepad. (large font).

    But I don't know how.  datagridview.?what=textbox1.text

    thanks Jere


    Jere Small

  • Friday, March 02, 2012 8:17 PM
     
     

    thanks Dave, I want the game player to be able due data entry, not just the programmer.  Thats why I want the textbox's.  See the other respose I made  to sygrien, that has some large print in it.

    thanks jere


    Jere Small

  • Friday, March 02, 2012 8:36 PM
     
      Has Code

    Try something like this. If you want default values to be editable by the user, then simply replace the string values with textbox values.

            For Each r As DataGridViewRow In Me.DataGridView1.Rows
                For Each c As DataGridViewCell In r.Cells
                    If String.IsNullOrEmpty(c.Value.ToString) Then
                        Select Case c.ColumnIndex
                            Case 2      'Third column
                                c.Value = "default value for column 3"
                            Case 4      'Fifth column
                                c.Value = "default value for column 5"
                        End Select
                    End If
                Next
            Next
    

  • Friday, March 02, 2012 10:12 PM
     
      Has Code

    I made the changes the form never loads now and  I get an error, when the form is called, see below.  Jere ;(

    Private Sub PlaysDataGridView_RowsAdded For Each r As DataGridViewRow In Me.PlaysDataGridView.Rows For Each c As DataGridViewCell In r.Cells If String.IsNullOrEmpty(c.Value.ToString) Then Select Case c.ColumnIndex Case 1 '2ND TEXT c.Value = TextBox1.Text Case 2 '3RD AB c.Value = TextBox2.Text Case 3 '4TH R c.Value = TextBox3.Text Case 4 '5th H c.Value = TextBox4.Text Case 5 '6th DB c.Value = TextBox5.Text Case 6 '7TH TP c.Value = TextBox6.Text Case 7 '8TH HR c.Value = TextBox7.Text Case 8 '9TH RBI c.Value = TextBox8.Text Case 9 '10TH BB c.Value = TextBox9.Text Case 10 '11TH SO c.Value = TextBox10.Text Case 11 '12TH CS c.Value = TextBox11.Text Case 12 '13TH SB c.Value = TextBox12.Text Case 13 '14TH E c.Value = TextBox13.Text Case 14 '15TH A c.Value = TextBox14.Text Case 15 '16TH PO c.Value = TextBox15.Text Case 16 '17TH BOE c.Value = TextBox16.Text Case 17 '18TH HBP c.Value = TextBox17.Text Case 18 '19TH OUTS c.Value = TextBox18.Text End Select End If Next Next End Sub ********************************************** ERROR ********************************************** PlayEntry.show() 'My data Entry Form InvalidOperationException was unhandled An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object.

    msdn suggests help?

    Get general help for this exception. innerException: Use the 'new' keyword to create an object instance innerException: Check to determine if the object is null before calling the method.

    ( the value of the new row cells would be NULL) Get general help for the inner exception.

    or - no help I can understand !



    Jere Small

  • Saturday, March 03, 2012 8:31 AM
     
     

    Jere,

    You may have placed your procedure BEFORE the form objects have been initialized. Therefore, any reference to the datagridview or the textboxes will fail.

    To be sure, when the error occurs, Visual Studio should show the line in error. Hover the variables with your mouse, you will see their values. One of them contains no value and provokes an error.

    In my humble opinion, you should run the procedure AFTER the user has entered data. It is much easier for a user to spot required data among empty cells than to visually check data when all cells contain values. So I would place the procedure just before saving the data back to the DB.

  • Saturday, March 03, 2012 11:13 AM
     
     
    I move the sub above the save sub routine. Same result.  The error occurs when I push the button to open the data entry form. It never leaves the start up form.  or the error occurs in the code section of the start up form not the data entry form. Jere

    Jere Small

  • Saturday, March 03, 2012 12:06 PM
     
     

    Jere,

    If you have moved the procedure away and the form still doesn't load, then it could be something else....

    1. In the "Solution Explorer", click the "Show All Files" button (look in the upper part of the Solution Explorer)
    2. Locate your form. Open the file which has the name of your form, followed by ".Designer.vb". This is a file automatically generated which contains the definition of the controls on your form.
    3. Locate the "InitializeComponent" procedure of your form
    4. Comment out the "<System.Diagnostics.DebuggerStepThrough()>" attribute
    5. Place a breakpoint at the beginning of your "InitializeComponent" procedure
    6. Run your app

    The breakpoint will hit, allowing you to follow the code and identify the line causing this error. 

  • Saturday, March 03, 2012 10:45 PM
     
      Has Code

    Ok I did it. here is the flow and then I clicked call stack to get the line #.

    It's all one long line in the error area: Jere º¿ º

    fieldform.designer.vb field.vb (formfield pops up) - i then click on Enter Plays bytteon -then field.vb playentry.show() 'line 297 > Bigger Leaguer Baseballer.exe!Bigger_Leaguer

    _Baseballer.Field.btnEnterPlay_Click (Object sender = {Text = "Enter Plays"},

    System.EventArgs e = {X = 23 Y = 8 Button = System.Windows.Forms.MouseButtons.Left}) Line 297


    Jere Small


    • Edited by JereTheBear Sunday, March 04, 2012 6:19 AM
    •  
  • Saturday, March 03, 2012 11:21 PM
     
     
    Please have a look at your message. Do you think this is understandable for anyone? It would be kind of you if you'd take the time and correct your message until it is ready to be sent. Thank you!

    Armin


  • Sunday, March 04, 2012 11:52 AM
     
     Answered

    Jere,

    Armin is right. We're ready to help, but you should help us too...

    There is an error somewhere. Please locate the line which causes the error and try to understand it by yourself. If you can't, then post the appropriate piece of code.

    As far as I understand the situation, you form doesn't load because of an exception "reference not set to an instance of an object". This has happened to me before in the following situation :

    • At design time, in the controls properties window, you set the value of a control or check a check-box or check a radio-button.
    • An event is assigned to this control, for example when the check-box is checked.
    • The code started when the event fires references an object which doesn't yet exists at the time the form is created.

    So when you start the form, the simple fact that one control has a value fires the event. The event causes an error because it references a non-existing object.

    Maybe you are in a similar case.