Small is New Big in C#

Coding

Datagridview refresh contents…

leave a comment »

hello, i have a datagridview and i add edit and delete
it’s values… and after every change i do
this.myTableAdapter.Fill(this.myDataSet.my);

this updates my datagrid successfully but if i had a row
selected before update in the datagridview after the update no row is
selected and it goes to top (if there is a scrollbar)… but i don’t
want to lose my selection and i want to stay at current position…

    namespace program
{
public partial class frmMain : Form
{
public int focusRow;
public bool boolRow;

private void frmMain_Load(object sender, EventArgs e)
{
//bound your data to datagirdview
{ code }

if(boolRow)
{
datagridview.FirstDisplayedScrollingRowIndex = focusRow;
datagridview.Rows[focusRow].Selected = true;
}
}

private void buttonEdit_Click(object sender, EventArgs e)
{
//your edit and update database code
{ code }

focusRow = datagridview.CurrentRow.Index;

boolRow = true;

frmMain_Load(sender,e);
}

}
}
Advertisements

Written by nauhil

June 14, 2010 at 10:56 am

Posted in Q&A

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: