lbsi
asked on
VB.Net Visual Studio 2005: DataGridView
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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
ASKER
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
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
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
ASKER
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
The rest of my above comment is accurate.
Appreciate the help,
Ed
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
Roger
ASKER
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;Initi al 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
Dim myConnect As New SqlConnection("Data Source=NOTEBOOK_DEV1;Initi
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
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
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
DataGridView.CurrentCell = DataGridView.Rows(0).Cells
should be
DataGridView.CurrentCell = DataGridView.Rows(0).Cells
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
ASKER
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
Roger:
Indeed specifying
DataGridView.CurrentCell = DataGridView.Rows(0).Cells
does produce an error.
Using
DataGridView.CurrentCell = DataGridView.Rows(0).Cells
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
ASKER
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
DataGridView.CurrentCell = DataGridView.Rows(0).Cells
puts the focus on the User Entry column which is the last in the grid.
Now, that is really interesting.
Thanks,
Ed
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").Di splayIndex = 0
.Columns("U_RptSelName").D isplayInde x = 1
.Columns("User Entry").DisplayIndex = 2
.Columns("U_RptSelTyp").Di splayIndex = 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
DataGridView.DataSource = table
DataGridView.CurrentCell = DataGridView.Rows(0).Cells
at the end of your code with this
DataGridView.DataSource = table
With DataGridView
.Columns("U_RptSelLbl").Di
.Columns("U_RptSelName").D
.Columns("User Entry").DisplayIndex = 2
.Columns("U_RptSelTyp").Di
End With
DataGridView.CurrentCell = DataGridView.Rows(0).Cells
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
Roger
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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