Share via


How to keep datagridview focus on the last row?

Question

Thursday, September 9, 2010 12:13 AM

I bind a datatable to a datagridview.

dataGridViewLog.DataSource = datatable;

When I update the datatable, the datagridview will update also.

Note: datatable is update in the other class, not in the mainform class which has the datagridview.

for(int i=0;i<100;i++)

{

    datatable.Rows.Add(i.ToString());

}

 

I want to datagridview keep focus on the last row when datatable add a new row.

How to do?

Thanks.

All replies (10)

Thursday, September 9, 2010 2:16 AM ✅Answered

Ok, I added a test Column and test rows to your code, remove it, I needed it here to run the project

Here's an example a Form with one button and one DataGridView:

 public partial class Form1 : Form
 {
 public Form1()
 {
  InitializeComponent();
  dataGridViewLog.DataSource = Utility.datalog;
  //test column
  DataColumn c = new DataColumn("öslkf", typeof(System.String));
  Utility.datalog.Columns.Add(c);
 }

 private void button1_Click(object sender, EventArgs e)
 {
  TestFlow1 t = new TestFlow1(this.dataGridViewLog);
  t.TestSequence();
 }
 }

 public static class Utility
 {
 //test table
 public static DataTable datalog = new DataTable();

 public static void LogPrint(string strLog)
 {
  //test row
  DataRow dr = datalog.NewRow();
  dr[0] = "afklöasdk";
  datalog.Rows.Add(dr);
 }
 }
 public class TestFlow1
 {
 DataGridView _dgv = null;
 public TestFlow1(DataGridView dgv)
 {
  _dgv = dgv;
 }
 public void TestSequence()
 {
  for (int i = 0; i < 100; i++)
  {
  if (_dgv != null)
  {
   Utility.LogPrint(i.ToString());

   this._dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;//maybe not needed
   //Application.DoEvents();
   if (this._dgv.Rows.Count > 0)
   {
   this._dgv.Rows[this._dgv.Rows.Count - 1].Selected = true;
   this._dgv.FirstDisplayedScrollingRowIndex = this._dgv.Rows.Count - 1;
   }
  }
  }
 }
 }

 regards,

  Thorsten


Thursday, September 9, 2010 3:04 AM ✅Answered

Thank Thorsten.

Your code is very helpful to me.

Could the DataGridView _dgv and focus the last row function move to class Utility?

Yes.

  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
      dataGridViewLog.DataSource = Utility.datalog;
      //test column
      DataColumn c = new DataColumn("öslkf", typeof(System.String));
      Utility.datalog.Columns.Add(c);
    }

    private void button1_Click(object sender, EventArgs e)
    {
      TestFlow1 t = new TestFlow1();
      Utility.DGV = this.dataGridViewLog;
      t.TestSequence();
    }
  }

  public static class Utility
  {
    public static DataGridView DGV { get; set; }
    //test table
    public static DataTable datalog = new DataTable();

    public static void LogPrint(string strLog)
    {
      //test row
      DataRow dr = datalog.NewRow();
      dr[0] = "afklöasdk";
      datalog.Rows.Add(dr);

      if (DGV != null)
      {
        if (DGV.Rows.Count > 0)
        {
          DGV.Rows[DGV.Rows.Count - 1].Selected = true;
          DGV.FirstDisplayedScrollingRowIndex = DGV.Rows.Count - 1;
        }
      }
    }
  }
  public class TestFlow1
  {
    public void TestSequence()
    {
      for (int i = 0; i < 100; i++)
      {
          Utility.LogPrint(i.ToString());
      }
    }
  }

regards,

  Thorsten


Thursday, September 9, 2010 12:28 AM

Hi,

perhaps needed:

 this.dataGridViewLog.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

and then:

 try
 {
 this.dataGridViewLog.Rows[this.dataGridViewLog.Rows.Count - 1].Selected = true;
 }
 catch
 {

 }

If you want to, reset the SelectionMode here.

regards,

  Thorsten

 

set the last row selected each time you add a DataRow to the table.

 


Thursday, September 9, 2010 12:41 AM

Thank you, thorsten.

Because my datatable is updated in a other class, not in mainform class.

So, where am i use "this.dataGridViewLog.Rows[this.dataGridViewLog.Rows.Count - 1].Selected = true; "


Thursday, September 9, 2010 1:05 AM

Thanks.

Could you give me some example about how to "either set the Modifiers-Property of the DGV to internal or add a public Property to the class holding the DGV"?


Thursday, September 9, 2010 1:28 AM

In the Designer click the DataGridView and go to the Properties-Window, select "Modifiers" and set it to - "what you need".

OR

//deleted, code will be posted in new message...

Edit: I'll post some real code soon, it's getting late here, so things might "come over not too clearly..."


Thursday, September 9, 2010 1:50 AM

Ok, I drank a coffee,

Create a new winForms-project: two Forms (Form1 and Form2) onto each a button and a DataGridview to Form1.

Form1 code:

  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      Form2 frm = new Form2(this.dataGridView1);
      frm.Show();
    }
  }

Form2 code:

  public partial class Form2 : Form
  {
    DataGridView _dgv = null;
    DataTable _t = new DataTable();

    public Form2(DataGridView dgv)
    {
      InitializeComponent();
      _dgv = dgv;
      DataColumn dc = new DataColumn("saDKL", typeof(System.String));
      _t.Columns.Add(dc);
      _dgv.DataSource = _t;
    }

    private void button1_Click(object sender, EventArgs e)
    {
      if (_dgv != null)
      {
        for (int i = 0; i < 10; i++)
        {
          DataRow dr = _t.NewRow();
          dr[0] = "dfk";
          _t.Rows.Add(dr);
          this._dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
          Application.DoEvents();
          if (this._dgv.Rows.Count > 0)
          {
            this._dgv.Rows[this._dgv.Rows.Count - 1].Selected = true;
          }
        }
      }
    }
  }

but I don't know, how to scroll the DGV...

regards,

  Thorsten


Thursday, September 9, 2010 1:55 AM

> but I don't know, how to scroll the DGV...

ok, found out, that's easy:

      this._dgv.FirstDisplayedScrollingRowIndex = this._dgv.Rows.Count - 1;

Thursday, September 9, 2010 1:58 AM

<pre lang="x-c#">public partial class MainForm : Form
{
  public MainForm()
  {
    dataGridViewLog.DataSource = Utility.datalog;
  }
}
public static class Utility
{
  public static DataTable datalog;
  
  public static void LogPrint(string strLog)
  {
    datalog.Rows.Add(strLog);
  }
}
public class TestFlow1
{
  public void TestSequence()
  {
    for(int i=0;i<100;i++)
    {
      Utility.LogPrint(i.ToString());
    }
  }
}

This is my code. Thanks.


Thursday, September 9, 2010 2:33 AM

Thank Thorsten.

Your code is very helpful to me.

Could the DataGridView _dgv and focus the last row function move to class Utility?