Solved

VB.Net Visual Studio 2005: DataGridView

Posted on 2006-10-26
13
1,064 Views
Last Modified: 2011-09-20
Good Afternoon

I am working with a DataGridView in vb.net Visual Studio 2005.  A member named Carl Twan helped me get a column, for "User Entry" inserted into the Grid amongst those
columns populated by a DataTable.  When I type in a cell for that column and then exit the cell, the text goes way.  At Carl's suggestion, I scoured my properties for the control and
could not find any offending settings.  

Is there a programmatic setting I can use that may ensure the data remains in the cell?

Also, with Carl's help, I have the first cell of this "User Entry" column as the current cell.  While it does indicate where the User needs to type the data, is there a way to change the
blue color of the cell to just the blinking cursor so I do not have to double click?

Thanks,
Ed

0
Comment
Question by:lbsi
  • 7
  • 5
13 Comments
 
LVL 13

Assisted Solution

by:newyuppie
newyuppie earned 50 total points
Comment Utility
since you are not getting any answers, may i suggest a link
http://www.awprofessional.com/articles/article.asp?p=446453&seqNum=14&rl=1

has a LOT of useful information on datagridview maybe it helps you

NY
0
 

Author Comment

by:lbsi
Comment Utility
Thanks...I will check it out.

I am beginning to wonder if the DataGridView is the right control for what I need to do.  I am finding it very difficult to work with.

Here is all I need to do if you do not mind...

I need to build an app that captures all the required parameters for a Crystal Report.  Instead of having to go to a new screen
for each parameter, we have entered all the parameters into a SQL table.  I need to load that data from a table to a control for the user.  
Then give provisions to have the parameter value entered. Then do some validating and call the Crystal Report.  

Conceptually it appeared very feasible.  So, I built a model with a known amount of parms.  I loaded the 2 parms into labels. Had textboxes
next to the labels for data entry.  Then passed the tetbox values as parms to the Crystal Report with no probems.  

I looked at the ListView control but I could not figure out how to get one column to be editable for the Data Entry values.  

Any opinions/insight would be helpful...

Thanks,
Ed
0
 
LVL 34

Expert Comment

by:Sancler
Comment Utility
Ed

I think the DataGridView might be right for your purposes, but I think you might need to handle your user entry column in a slightly different way from that suggested in your other question.  Rather than trying to add an unbound column to your DataGridView, you could add a column to your datatable.  Using the code from your other post

     Dim myQuery As String = "Select U_RptSelLbl,U_RptSelName,U_RptSelTyp from [@LBSIRSEL] where U_RptMenuID = '" & Menu & "'"
     Dim table As New DataTable

        Dim adapter As New SqlDataAdapter(myQuery, myConnect)
        adapter.Fill(table)
        DataGridView.DataSource = table

try sticking these lines

        Dim userCol As New DataColumn("User Entry", GetType(String))
        table.Columns.Add(userCol)

immediately after

     Dim table As New DataTable

or between

        adapter.Fill(table)

and

        DataGridView.DataSource = table

The former should add the new column as the first one in the table.  The latter should add it as the last column.  It assumes that what the user will enter is a string.  The datagridview should then show it automatically (in whichever position you have chosen) and any entry that the user makes in it should persist.

It would be possible for the new column to be shown in some intermediate position in the datagridview, but that gets a bit more complicated.  

First of all, try one or other of the suggestions above and see if - operationally - that produces what you want.  Then we can further consider display questions if necessary.

Roger
0
 

Author Comment

by:lbsi
Comment Utility
Thanks for the reply Roger...

After implementing your suggestion, I am pleased to see the new column inserted where I need it...after the 2nd and before the final column.  
I may have something wrong, as you indicated it should appear in the first slot.  It appears the focus is on the first row/column as indicated by the
blue background color in the cell.  

What do you think?  Should I take the column insert how it is now?  Or should we implement the code to make it appear in the 3rd slot?

To get around the initial problem of getting the column to appear, I just added a blank column to the db.  While it was not the right way to do
it, I was able to continue with the application.  With that I was able to get the focus, blue background color, to be on the first cell of that
added column using the following code:

DataGridView.CurrentCell = DataGridView.Rows(0).Cells(2)

However, that line does not move the focus from that first cell of the first row/column to the 3rd column when I programmatically add the User
column.

Can you help with the lines of code required to get the User column inserted in the 3rd slot?

Thanks,
Ed
0
 

Author Comment

by:lbsi
Comment Utility
My bad Roger...the column is getting appended to the end of the grid in both cases of code positioning that you suggested.

The rest of my above comment is accurate.

Appreciate the help,
Ed
0
 
LVL 34

Expert Comment

by:Sancler
Comment Utility
Can you please post all the code, as it now stands, in relation to filling the datatable, adding any columns to the datatable, binding the datatable to the datagridview and trying to set the focus to the cell that you want?  I have, from your original post and your latest post, got a general idea of what is happening, but the Devil is often in the detail and I am not sure I've completely grasped all that.

Roger
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:lbsi
Comment Utility
       Dim myQuery As String = "Select U_RptSelLbl,U_RptSelName,U_RptSelTyp from [@LBSIRSEL] where U_RptMenuID = '" & Menu & "'"
        Dim myConnect As New SqlConnection("Data Source=NOTEBOOK_DEV1;Initial Catalog=3D;Integrated Security=True")
        Dim table As New DataTable
        Dim userCol As New DataColumn("User Entry", GetType(String))
        table.Columns.Add(userCol)
        Dim adapter As New SqlDataAdapter(myQuery, myConnect)
        adapter.Fill(table)
        DataGridView.DataSource = table
        DataGridView.CurrentCell = DataGridView.Rows(0).Cells(4)

I had neglected to take out the previously defined column from when I had the blank field for the column coming in from the db.  
So, the focus is not in the first cell anymore.  However, it will now not let me specify the 4th cell like I wanted to in the last line of
code.  It is behaving like it does not know about a column defined in code.

Thanks for the help,
Ed
0
 
LVL 34

Expert Comment

by:Sancler
Comment Utility
That code looks OK to me except for one thing.  The table (hence grid) should have 4 fields/columns: the three specified in the SQL query and the one that was added.  The index for those is zero-based, so if you want to reference the fourth column/cell you should use 3, not 4.  Which means that

        DataGridView.CurrentCell = DataGridView.Rows(0).Cells(4)

should be

        DataGridView.CurrentCell = DataGridView.Rows(0).Cells(3)

Indeed, I'm surprised that using 4 did not produce an error.

On that code the order of the columns in the grid should be

User Entry
U_RptSelLbl
U_RptSelName
U_RptSelTyp

Is that what you want?  If not, will you please say - with a list like that - what you want the order to be?

Roger
0
 

Author Comment

by:lbsi
Comment Utility
Good Morning

Roger:

Indeed specifying
     DataGridView.CurrentCell = DataGridView.Rows(0).Cells(4)
does produce an error.

Using
     DataGridView.CurrentCell = DataGridView.Rows(0).Cells(3)
gets me the third colum in the grid.

That behavior is really rewildering.  When using 4 it is like it does not know about the column we added in the code.

Here is the order I am looking for:

U_RptSelLbl
U_RptSelName
User Entry
U_RptSelTyp

Thanks for the help,
Ed

0
 

Author Comment

by:lbsi
Comment Utility
Specifying
     DataGridView.CurrentCell = DataGridView.Rows(0).Cells(0)
puts the focus on the User Entry column which is the last in the grid.

Now, that is really interesting.

Thanks,
Ed
0
 
LVL 34

Expert Comment

by:Sancler
Comment Utility
Replace

        DataGridView.DataSource = table
        DataGridView.CurrentCell = DataGridView.Rows(0).Cells(4)

at the end of your code with this

        DataGridView.DataSource = table
        With DataGridView
            .Columns("U_RptSelLbl").DisplayIndex = 0
            .Columns("U_RptSelName").DisplayIndex = 1
            .Columns("User Entry").DisplayIndex = 2
            .Columns("U_RptSelTyp").DisplayIndex = 3
        End With
        DataGridView.CurrentCell = DataGridView.Rows(0).Cells(0)

The bit "With DataGridView" puts the columns in the order that you want.  My guess - from your statement that "the User Entry column which is the last in the grid" - is that you might already have done something like the above becuase, just on the code shown, it should be the first in the grid.

As your last post highlights, the new column - "User Entry" - is referenced by 0.  That is because we added it BEFORE the sql query created and populated the other columns in the table, so its index in the TABLE (and therefore in the underlying DataRowView) is 0.  The re-ordering of the columns using .DisplayIndex as above does not alter that: that just alters the order in which the columns are DISPLAYED.  To avoid the sort of confusion that this can create, it might be better to reference the column that you want by name, rather than its numerical index.  Like this

        DataGridView.CurrentCell = DataGridView.Rows(0).Cells("User Entry")

Roger
0
 

Author Comment

by:lbsi
Comment Utility
Excellent...

All is well now.  The User Entry column is getting default set as the current cell with the blue background.  
Is it possible to set that focus with the blinking cursor instead of the blue background?  I am digging but have not
come across anything yet.

Thanks,
Ed

0
 
LVL 34

Accepted Solution

by:
Sancler earned 450 total points
Comment Utility
Add

        DataGridView.BeginEdit(True)

immediately after

        DataGridView.CurrentCell = DataGridView.Rows(0).Cells("User Entry")

The "True" makes it select all the text that is currently in the cell.  If you don't want to do that, change it to "False".

Roger
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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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 …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

762 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

7 Experts available now in Live!

Get 1:1 Help Now