我在哪里放置最后添加记录的突出显示代码?

时间:2011-11-25 13:19:54

标签: c# winforms focus

我的计划有两种形式 - MainForm(Mainform)SubForm(Add_Student)MainFormStudent_DataGridView,其中显示了学生的所有记录。 SubForm(AddStudent)新增学生。

我的任务是添加SubForm(Add_Students)的新学生。我做到了!:)

下一个任务是突出显示Student_DataGridView MainForm上的最新最新记录。我做到了!:)

但我不知道在哪里放置最后添加记录的突出显示代码。我试图参加活动:

private void MainForm_Activated(object sender, EventArgs e)
{
  Student_DataGridView.Rows.Count - 1.Selected = true
}

但它总是在显示MainForm时起作用。我只需要在添加新记录后显示。哪个是活动?

或者我在哪里更好地设置代码?

2 个答案:

答案 0 :(得分:1)

您需要将行标记为新行。 我不知道他们有多久了:

  • 添加
  • 后首次重新加载列表
  • 直到app关闭
  • 一段时间......

取决于具有不同的解决方案。 如果您只在应用程序运行时需要它,您可以使用该行的Tag属性并在其中创建一个标志并在其不再是新的后删除它。 最简单的是:

void SetTag(DataRow row)
{
    row.Tag = true;
}


if(row.Tag != null && row.Tag == true ) Highlight(row);

如果它应该持续超过一个启动/在不同的应用程序实例上,我将在表中创建一个新字段来存储标志,创建日期或适合您在数据库中的需求并使其持久化。

答案 1 :(得分:0)

我找到了解决方案:)。也许这段代码对你有用,所以:

public int student_count_row = 0;

 private void MainForm_Load(object sender, EventArgs e)
 { 
   this.StudentTableAdapter.Fill(this.registrationDataSet. Student );  
   student_count_row= Student DataGridView.Rows.Count;
 }


 private void MainForm_Activated(object sender, EventArgs e)
 {
   this.StudentTableAdapter.Fill(this.registrationDataSet. Student );
   int Student _row = Student DataGridView.Rows.Count;
 if ( Student p_count_row ==   Student_row)
 {
        //We do never here in this condition
             ;
 }
   if ( Student _row > Student _count_row)
 {
 int k = 0;
   k = Student DataGridView.Rows.Count - 1;
   this.StudentDataGridView.CurrentCell = this.StudentDataGridView[2, k];
   student_count_row =   student_row;
 } 


 }

enter code here