Datagridview question
Datagridview question
Datagridview question
Datagridview question
Datagridview question
Datagridview question Datagridview question Datagridview question Datagridview question Datagridview question Datagridview question Datagridview question Datagridview question
Datagridview question Datagridview question
Datagridview question
Go Back  Xtreme Visual Basic Talk > > > Datagridview question


Reply
 
Thread Tools Display Modes
  #1  
Old 09-30-2007, 11:42 PM
YaRoBi YaRoBi is offline
Regular
 
Join Date: Feb 2004
Location: Belgium - Aalst
Posts: 69
Question Datagridview question


I thought it was possible if I doubleclick in a datagridview to get the value of the first column of that row. Only I can't figure it out how

can someone help me with it?

Each row contains patientid - naam - voornaam - geboortedatum - ...
and I need to select the patientid to popup a new screen with the details of that person.

Last edited by YaRoBi; 10-01-2007 at 12:04 AM.
Reply With Quote
  #2  
Old 10-01-2007, 10:09 AM
IUnknown's Avatar
IUnknown IUnknown is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Oct 2004
Location: Montréal
Posts: 1,135
Default

Hi YaRoBi

You can handle the DataGridView1 CellDoubleClick event.

The DataGridViewCellEventArgs of this event contains the RowIndex and ColumnIndex of the cell that was dblclicked. Use the RowIndex and a value of 0 for the column index to obtain the value in the first column, e.g.
Code:
MessageBox.Show(DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString())
Where e is the event argument.
__________________
win7 : vs 2008 : .Net 3.5
Reply With Quote
  #3  
Old 10-02-2007, 04:24 AM
YaRoBi YaRoBi is offline
Regular
 
Join Date: Feb 2004
Location: Belgium - Aalst
Posts: 69
Default

Quote:
Originally Posted by IUnknown View Post
Hi YaRoBi

You can handle the DataGridView1 CellDoubleClick event.

The DataGridViewCellEventArgs of this event contains the RowIndex and ColumnIndex of the cell that was dblclicked. Use the RowIndex and a value of 0 for the column index to obtain the value in the first column, e.g.
Code:
MessageBox.Show(DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString())
Where e is the event argument.

thx but I would like to have the user doubleclick in the greybar at the left site of the datagrid, you know where the record selector is?

Is that possible?
Attached Images
File Type: jpg screen.jpg (193.1 KB, 17 views)
Reply With Quote
  #4  
Old 10-02-2007, 09:24 AM
IUnknown's Avatar
IUnknown IUnknown is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Oct 2004
Location: Montréal
Posts: 1,135
Default

Use the DataGridView RowHeaderMouseDoubleClick event.
__________________
win7 : vs 2008 : .Net 3.5
Reply With Quote
  #5  
Old 10-03-2007, 04:01 AM
YaRoBi YaRoBi is offline
Regular
 
Join Date: Feb 2004
Location: Belgium - Aalst
Posts: 69
Default

Quote:
Originally Posted by IUnknown View Post
Use the DataGridView RowHeaderMouseDoubleClick event.
thx a lot

You'r the man

But maybe you can also help me with an other thing I would like to change in my 'program'.

Rightnow I set the DataSource in the properties but I would like to program them because I would make a searchoption so the users can type a part of the name. And when they enter the search buttom the datagridview is filled with only the possible records. Or can I just do this by programming something like datgv.datasource= "select patientid,naam,... from patienten where naam is like "var" ?

If so, how do I set back the datagridview content after they clicked the cancelbuttom / filter out



da
Reply With Quote
  #6  
Old 10-03-2007, 07:22 PM
IUnknown's Avatar
IUnknown IUnknown is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Oct 2004
Location: Montréal
Posts: 1,135
Default

Make "var" a parameter. Create a parameterized query or a stored procedure that takes a parameter to add a search criterion.

A brief intro to
Create TableAdapter Queries

Search Criteria Builder Dialog Box can be used to add a parameterized query.
__________________
win7 : vs 2008 : .Net 3.5

Last edited by IUnknown; 10-04-2007 at 10:01 AM. Reason: Corrected spelling and added a link
Reply With Quote
  #7  
Old 10-05-2007, 01:16 PM
IUnknown's Avatar
IUnknown IUnknown is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Oct 2004
Location: Montréal
Posts: 1,135
Default

I'd forgotten about your last question.

In the Where part of your SQL string where you have added your parameter, if you've used the equal sign '=' then replace '=' with 'LIKE'. Then you can pass the SQL wildcard symbol, i.e., a percent sign '%' to the parameter to retrieve all rows.
__________________
win7 : vs 2008 : .Net 3.5
Reply With Quote
  #8  
Old 10-08-2007, 01:51 AM
YaRoBi YaRoBi is offline
Regular
 
Join Date: Feb 2004
Location: Belgium - Aalst
Posts: 69
Question

Quote:
Originally Posted by IUnknown View Post
Make "var" a parameter. Create a parameterized query or a stored procedure that takes a parameter to add a search criterion.

A brief intro to
Create TableAdapter Queries

Search Criteria Builder Dialog Box can be used to add a parameterized query.
Code:
Private Sub MDIBehandelingen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MdbPath = "d:/Tandjes/TLC.mdb"
        strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & MdbPath & ";Persist Security Info=False"
        MakeCon(strCon)
        txtPatientid.Text = Patient
        strSql = "SELECT * FROM tbl_Patienten WHERE patientid= " & Patient
        'strSql = "SELECT tbl_Behandeling.datum, tbl_Behandeling.tand, tbl_Behandeling.behandeling, tbl_Behandeling.opmerking, tbl_Behandeling.verstrekker, tbl_Behandeling.attachement FROM tbl_Behandeling WHERE tbl_Behandeling.patientid= " & Patient
        MakeCon(strCon)
        RS = New ADODB.Recordset
        RS = MyCon.Execute(strSql)
        txtNaam.Text = RS.Fields(1).Value
        txtvoornaam.Text = RS.Fields(2).Value
        txtGeboortedatum.Text = RS.Fields(3).Value
        txtAdres.Text = RS.Fields(4).Value
        txtPostcode.Text = RS.Fields(5).Value
        txtGemeente.Text = RS.Fields(6).Value
        txtTelefoon.Text = RS.Fields(7).Value
        txtTelWerk.Text = RS.Fields(9).Value
        txtGSM.Text = RS.Fields(8).Value
        txtEmail.Text = RS.Fields(10).Value
        txtMutualiteit.Text = RS.Fields(11).Value
        txtRecall.Text = RS.Fields(14).Value
        txtTerugbetaling.Text = RS.Fields(19).Value
        txtCommunicatie.Text = RS.Fields(15).Value
        txtOpmerking.Text = RS.Fields(24).Value
        txtHuisdokter.Text = RS.Fields(22).Value

        dgvBehandelingen.DataSource = RS.DataSource
    End Sub
this is my code and It works fine until there is an empty field in the recordset. So when there is no Telephonenumbre it gives an error and I can't fill my datagridview dgvBehandelingen with data.

How can I prevent the error when the field is empty and how can I fill my datagridview?
Reply With Quote
  #9  
Old 10-09-2007, 07:45 AM
YaRoBi YaRoBi is offline
Regular
 
Join Date: Feb 2004
Location: Belgium - Aalst
Posts: 69
Default

The problem with the errors I have fixed it myself *proud*

but I still can't fill the datagridview .

I have my query and recordset but it just won't work

Code:
MdbPath = "d:/Tandjes/TLC.mdb"
        strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & MdbPath & ";Persist Security Info=False"
        'MakeCon(strCon)
        txtPatientid.Text = Patient
        strSql = "SELECT * FROM tbl_Patienten WHERE patientid= " & Patient
        'strSql = "SELECT tbl_Behandeling.datum, tbl_Behandeling.tand, tbl_Behandeling.behandeling, tbl_Behandeling.opmerking, tbl_Behandeling.verstrekker, tbl_Behandeling.attachement FROM tbl_Behandeling WHERE tbl_Behandeling.patientid= " & Patient
        MakeCon(strCon)
        RS = New ADODB.Recordset
        RS = MyCon.Execute(strSql)
        PatientDetailsOphalen()   ' fill the textboxes

        dgvBehandelingen.DataSource = RS.DataSource  'should fill the datagrid
Reply With Quote
  #10  
Old 10-09-2007, 09:16 PM
IUnknown's Avatar
IUnknown IUnknown is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Oct 2004
Location: Montréal
Posts: 1,135
Default

I'm not sure if the DGV would take an ADODB recordset in the DataSource. Is there any particular reason you're not using ADO.Net to access your data? In VBA or VB6, ADO is great, but in .Net, ADO.Net is the way to go.

If you will be doing data in .Net, it would be worthwhile to take some time to go through some of the links on Data Walkthroughs and the ones posted earlier.
__________________
win7 : vs 2008 : .Net 3.5
Reply With Quote
  #11  
Old 10-10-2007, 04:30 AM
YaRoBi YaRoBi is offline
Regular
 
Join Date: Feb 2004
Location: Belgium - Aalst
Posts: 69
Default

Quote:
Originally Posted by IUnknown View Post
I'm not sure if the DGV would take an ADODB recordset in the DataSource. Is there any particular reason you're not using ADO.Net to access your data? In VBA or VB6, ADO is great, but in .Net, ADO.Net is the way to go.

If you will be doing data in .Net, it would be worthwhile to take some time to go through some of the links on Data Walkthroughs and the ones posted earlier.
I'm just using ADODB because I'm not used to ADO.net
last time I used VB was 3 years ago so I need to learn most of it again

I just can't find a way try all the help files, you don't have a little example that could help me?
Reply With Quote
  #12  
Old 10-10-2007, 03:38 PM
IUnknown's Avatar
IUnknown IUnknown is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Oct 2004
Location: Montréal
Posts: 1,135
Default

Here is a sample with code: How to: Bind Data to the Windows Forms DataGridView Control

Since you need to "learn most of it again", and as you're using VS2005, you might as well focus on ADO.Net rather than trying to re-learn ADO or learning how use ADO in .Net. And if you have questions, post them here on the forum.
__________________
win7 : vs 2008 : .Net 3.5
Reply With Quote
  #13  
Old 10-11-2007, 07:19 AM
YaRoBi YaRoBi is offline
Regular
 
Join Date: Feb 2004
Location: Belgium - Aalst
Posts: 69
Default

I copied the code in a new project but it won't work

somethings wrong with my connectionstring

edit:

my database is called TLC.mdb and is in the next directory D:\TLC\

can you tell me how to make my connectionstring with ado.net working?

Last edited by YaRoBi; 10-11-2007 at 07:33 AM.
Reply With Quote
  #14  
Old 10-11-2007, 09:15 AM
IUnknown's Avatar
IUnknown IUnknown is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Oct 2004
Location: Montréal
Posts: 1,135
Default

It appears that similar questions were posed on displaying data in a DataGridView control, so here is a fully commented working code for
connecting to an access database using OleDB, retrieving the data using a parameterized query and displaying the data on a DataGridView control.
Code:
Private Sub btnExecuteQuery_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles btnExecuteQuery.Click ' Define your connection string ' Note that as I've copied the DB to my project, I could use the shortcut '|DataDirectory|' ' for the DB path Dim bd1ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\bd1.mdb" ' Create the connection object Dim cnx As OleDb.OleDbConnection cnx = New OleDb.OleDbConnection(bd1ConnectionString) ' In Access DB, use the question mark '?' for parameter Dim qry As String = "Select Champ, Champ2 From Table1 Where (Id = ?)" ' Create the command object Dim cmd As OleDb.OleDbCommand cmd = New OleDb.OleDbCommand(qry, cnx) ' Next add a parameter to the command object ' and assign the parameter value cmd.Parameters.Add("@bd1_Id", OleDb.OleDbType.Integer).Value = Integer.Parse(tbxParameter.Text) ' Create a data adapter object to get the data Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(cmd) ' Next create a datatable to hold the data Dim dt As DataTable = New DataTable("Table1") ' Fill the datatable using the data adapter da.Fill(dt) ' Finally pass the data to the DGV. DataGridView1.DataSource = dt End Sub
For the Data Source in your connection string, use "Data Source=D:\TLC\TLC.mdb;"
__________________
win7 : vs 2008 : .Net 3.5

Last edited by IUnknown; 10-11-2007 at 06:03 PM. Reason: Add information
Reply With Quote
  #15  
Old 10-12-2007, 07:32 AM
YaRoBi YaRoBi is offline
Regular
 
Join Date: Feb 2004
Location: Belgium - Aalst
Posts: 69
Default

Ok my datagridview works fine
but since I'm using OLEDB ant not ADODB, I'm not having a recordset .

so I can't use txtNaam.text = Rs.field(1) anymore

how can I fill my textboxes with the data of this query

strSQL = select * from patient where patientid = varPatient
Reply With Quote
  #16  
Old 10-12-2007, 10:27 AM
IUnknown's Avatar
IUnknown IUnknown is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Oct 2004
Location: Montréal
Posts: 1,135
Default

In ADO.Net, the DataTable is 'kinda' like the equivalent to the ADO Recordset.

You can use something like this:
Code:
Dim row As Integer = 0 Dim col As Integer = 1 TextBox2.Text = dt.Rows(row).Item(col).ToString()
__________________
win7 : vs 2008 : .Net 3.5
Reply With Quote
  #17  
Old 10-15-2007, 07:30 AM
YaRoBi YaRoBi is offline
Regular
 
Join Date: Feb 2004
Location: Belgium - Aalst
Posts: 69
Default

Hey, guess what. It's working


an other question , can I make my datagridview the exact width of my colums?Or is that impossible?
Reply With Quote
  #18  
Old 10-15-2007, 05:03 PM
IUnknown's Avatar
IUnknown IUnknown is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Oct 2004
Location: Montréal
Posts: 1,135
Default

I'm glad you got it.

Do you mean autosizing the columns to fit on the DGV? You can use the column AutoSizeMode (DataGridViewAutoSizeColumnMode) or to set it manually you can use the column Width (DataGridViewColumn) property.
__________________
win7 : vs 2008 : .Net 3.5

Last edited by IUnknown; 10-15-2007 at 05:13 PM.
Reply With Quote
  #19  
Old 10-16-2007, 04:17 AM
YaRoBi YaRoBi is offline
Regular
 
Join Date: Feb 2004
Location: Belgium - Aalst
Posts: 69
Default

Quote:
Originally Posted by IUnknown View Post
I'm glad you got it.

Do you mean autosizing the columns to fit on the DGV? You can use the column AutoSizeMode (DataGridViewAutoSizeColumnMode) or to set it manually you can use the column Width (DataGridViewColumn) property.

I allready resized my columns but I wanted to make the datagridview
the exact size off the columns width.
Reply With Quote
  #20  
Old 10-16-2007, 04:56 PM
IUnknown's Avatar
IUnknown IUnknown is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Oct 2004
Location: Montréal
Posts: 1,135
Default

You can get the sum of the column widths by looping through the DGV column collection. Add to the sum the width of a few pixels (for borders) and assign the total to the DGV width.
__________________
win7 : vs 2008 : .Net 3.5
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Datagridview question
Datagridview question
Datagridview question Datagridview question
Datagridview question
Datagridview question
Datagridview question Datagridview question Datagridview question Datagridview question Datagridview question Datagridview question Datagridview question
Datagridview question
Datagridview question
 
Datagridview question
Datagridview question
 
-->