Link to home
Start Free TrialLog in
Avatar of Kevin Rea
Kevin ReaFlag for United States of America

asked on

C# winforms, how to show records in datagridview if there is more than one record from matching text field

Hi guys,

I am new to C#. I am experienced with VBA, though that really does not have that much to do with this..

I have a datagridview on my winform, that I want to only show with data if there is more than one record existing with the CallSign that
is currently on the form itself.

This is the code I have for the datagridview
***************************
 OleDbCommand command = new OleDbCommand();
            connection2.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\HamLogBookV10\hamlogbook.accdb;
Persist Security Info=False;";
            command.Connection = connection2;

            string query2 = "select CallSign, CallDate, FullName, [Band], Freq, Mode, City, State, Country From Main WHERE (CallSign = '"+txtCallSign.Text+"')";
           
            command.CommandText = query2;
            OleDbDataAdapter da = new OleDbDataAdapter(command);
            DataTable dz = new DataTable();
                da.Fill(dz);
                 mainDataGridView1.DataSource = dz;
****************************
so, what I want to happen is, on the form above this datagridview, are all the text fields.
the one field that I am interested in is  txtCallSign, so if it already has 1 or more existing records in the database file
with the same callsign, then I want to populate the datagridview with those records, otherwise, the datagridview does
not need to be populated.

thanks very much,
Kevin Rea
Lancaster, Calif.
ASKER CERTIFIED SOLUTION
Avatar of srikanthreddyn143
srikanthreddyn143

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of srikanthreddyn143
srikanthreddyn143

This way you are populating grid only if there is data returned from database.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think the points should all be given to srikanthreddyn143.  That poster was first to answer the actual question.  I only added some management to the code the OP posted.