Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7554497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:21:04+00:00 2026-05-30T11:21:04+00:00

I am using linq2sql to add a record to my datagridview and refreshing the

  • 0

I am using linq2sql to add a record to my datagridview and refreshing the grid. Right now after refresh it moves the row selection to the top. How would I tell it to highlight the most recent added record?

Not sure if this would help but I have included the code below I am using to find out the pk of the selected row. As what I would like to do is kind of the opposite. Thanks

        //get selected row index
        int index = this.dataGridView1.CurrentRow.Index;
        //get pk of selected row using index
        string cellValue = dataGridView1["pkColumn", index].Value.ToString();
        //change pk string to int
        int pKey = Int32.Parse(cellValue);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T11:21:05+00:00Added an answer on May 30, 2026 at 11:21 am

    If you know in advance that anytime a new item is added, it should appear at the bottom of the list, you could use:

        int index = this.dataGridView1.Rows.Count - 1;
        this.dataGridView1.Rows[index].Selected = true;
        this.dataGridView1.FirstDisplayedScrollingRowIndex = index;
    

    But, if your order the list by other column then you’re not going to get the desired results.

    I recommend you, is to search for the row-index you need to give the selection to, this could be performed by a function, let’s say int SearchRowIndexForPKValue(int pKey), that will perform a look-up within the cell values of the column that contains your PK/ID ("pkColumn") returning the row index. In such way the code above should looks like:

        int index =  SearchRowIndexForPKValue(pKey);
        this.dataGridView1.Rows[index].Selected = true;
        this.dataGridView1.FirstDisplayedScrollingRowIndex = index;
    

    EDIT: int SearchRowIndexForPKValue(int pKey) is not an existing built-in method of the DataGridView. You must declare it and implement it in your code; for the body of the method, you can get the code from this post.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The problem is "after inserting a data using linq2sql & do submit changes ,
When using Linq2sql everything automagically works. My experience is that going with the flow
so I'm using LinQ2SQL quite heavily in my current application, and although I have
I'm using Linq2Sql for my asp.net mvc project and so far it has worked
I'm using Linq2Sql with pipes/filters. My database consists of Accounts and Transactions tables where
Been succcesfully adding records using Linq2Sql with the linq2sql genereated classes, works great.. But
I am creating some tag functionality for a forum using linq2sql, and I have
I have a question of how to sort an anonymous type. Using Linq2SQL I
what is the preferred practice when linq2sql using (in asp.net mvc applications): to create
I'm trying to take a POCO object and update it with Linq2SQL using an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.