Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

Navigate Datagrid

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gina_Marano - 28 Jan 2008 04:57 GMT
Hey All,

I have the following code example where the user gives me a query and
I populate a grid:

       private void btnTestSQL_Click(object sender, EventArgs e)
       {
           DataSet ds = new DataSet();

           SqlConnection conn = new SqlConnection(sSrcConnectionStr);
           try
           {
               conn.Open();

               SqlDataAdapter da = new SqlDataAdapter(eSqlText.Text,
conn);
               da.Fill(ds, "data");
               gTest.DataSource = ds;
               gTest.DataMember = "data";
               ds = null;
               da = null;
           }
           finally
           {
               conn.Close();
               conn.Dispose();
           }
       }

Works great!

Next I want to navigate through the grid and as I navigate the current
row is highlighted. I know this is inefficient with large datasets but
I want the visual feedback to the user.

I found how to do this using the currencymanager but I also want the
physical grid row to be highlighted as I traverse the grid.

           CurrencyManager cm =
(CurrencyManager)this.BindingContext[gTest.DataSource];
           int rowCount = cm.Count;

Why does rowCount = 1 when I have 10 rows?

At design time I don't know what column a field value will be in but I
know that the field value must be in the datasource some where. So I
will need to be able to determine that as well.

Thanks much

~Gina_M~
Nicholas Paldino [.NET/C# MVP] - 28 Jan 2008 15:56 GMT
Gina,

   The count should not be 1 if you have multiple rows bound to the grid
(and it's not in a virtual mode).

   Can you show a complete example where this occurs?  Iterating through
the bindingmanager to iterate through the rows is correct to change the row
and have it reflect in the grid, but finding the column that the cursor is
on will require you to look at the Current (or CurrentCell property, I can't
remember the name offhad) property on the grid to find on which cell the
focus is on.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hey All,
>
[quoted text clipped - 47 lines]
>
> ~Gina_M~
Ignacio Machin ( .NET/ C# MVP ) - 28 Jan 2008 16:15 GMT
Hi,

You do know that you are wide open to SQL injection right?

What if the user enter :  "delete from table" ?

Signature

Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.

> Hey All,
>
[quoted text clipped - 47 lines]
>
> ~Gina_M~
Gina_Marano - 28 Jan 2008 19:25 GMT
> You do know that you are wide open to SQL injection right?

Thanks for the warning but this will be an internal tool used by
development. They have the credentials to drop the entire database if
they want.

I got the bad code example off of one of MS pages...

I found the problem:

CurrencyManager cm =
(CurrencyManager)this.BindingContext[gTest.DataSource];

I needed to include the DataMember as well:

CurrencyManager cm =
(CurrencyManager)this.BindingContext[gTest.DataSource,gTest.DataMember];

BUT I still need to get the field values for the row.

I assume that I can use:

DataGridViewRow row = gTest.CurrentRow;

to get the current row. Since i do not know the order of the field
names, how do I get a field/cell value by name rather than by
position?

Thanks

~Gina_M~
Gina_Marano - 28 Jan 2008 19:35 GMT
OK, I figured it out...

DataGridViewRow row = gridTest.CurrentRow;
string s = row.Cells["ID"].Value.ToString();

~Gina_M~

> > You do know that you are wide open to SQL injection right?
>
[quoted text clipped - 27 lines]
>
> ~Gina_M~

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.