2016-02-01 124 views
0

我正在使用Visual Studio 2012,并且已经制作了一个窗体应用程序,我正在使用一个datagridview来显示SQL数据库中的一个表的信息。如何添加上一个和下一个按钮到datagridview

我已经从datagridview行的表单加载信息自动直接进入文本框。

SqlDataAdapter SDA = new SqlDataAdapter("SELECT * FROM Stock", con); 
DataTable DATA = new DataTable(); 
SDA.Fill(DATA); 
dataGridView1.DataSource = DATA 

txtStock3.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString(); 
Descriptioncombo2.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString(); 
txtprice2.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString(); 

的问题是,我需要添加一个上一个按钮和下一个按钮,以便用户可以通过datagridview的行导航并看到一个DataGridView行的每一列在一个文本框的信息。我看过类似的问题,并通过互联网浏览寻找解决我的问题,但我似乎无法找到一种方式与我的代码。你也可以告诉我如何添加一行代码告诉用户,如果他们点击数据库的所有行,接下来将不再有行选择。

+1

很简单,你把它扔掉,然后在WPF和EF – user853710

+0

这就是我所说的。使用WPF – user853710

+0

你至少试过Googleing。看看Youtube。至少有100个视频向您展示,这种复杂程度以及如何通过点击来实现此目的 – user853710

回答

1

提供替换卡伦的解决方案,如果你喜欢/必须用按钮去浏览,那么你将要处理的CurrentCellChanged事件以及下面的按钮Click事件:

private void DataGridView1_CurrentCellChanged(object sender, EventArgs e) 
{ 
    if (this.dataGridView1.CurrentRow != null) 
    { 
     txtStock3.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString(); 
     Descriptioncombo2.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString(); 
     txtprice2.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString(); 

     this.prevButton.Enabled = this.dataGridView1.CurrentRow.Index > 0; 
     this.nextButton.Enabled = this.dataGridView1.CurrentRow.Index < this.dataGridView1.Rows.Count - 1; 
    } 
} 

private void PrevButton_Click(object sender, EventArgs e) 
{ 
    int prev = this.dataGridView1.CurrentRow.Index - 1; 
    this.dataGridView1.CurrentCell = this.dataGridView1.Rows[prev].Cells[this.dataGridView1.CurrentCell.ColumnIndex]; 
} 

private void NextButton_Click(object sender, EventArgs e) 
{ 
    int next = this.dataGridView1.CurrentRow.Index + 1; 
    this.dataGridView1.CurrentCell = this.dataGridView1.Rows[next].Cells[this.dataGridView1.CurrentCell.ColumnIndex]; 
} 

CurrentCellChanged事件将处理逻辑,如果您可以点击上一个下一个。它们各自的点击事件只是将当前单元向后或向前移动一行。

+0

谢谢我原本打算做一点点不同,因为如果没有更多的行时显示一个消息框,但我认为你的方式更好。谢谢OhBeWise你已经解决了我的问题,并感谢Karen Payne和user853710你的答案,这也是解决问题的有效方法。 – reddevil54

1

将网格中的comulmns配置为您的匹配类型。然后,在查询之后,将数据绑定到此GridView。你可以添加两个按钮,其中“下一步”按钮将获取当前选中的网格,并将其设置为选中的一个。以前会做相反的事情。这是一个小屁股疼痛。我讨厌WinForms中的网格。过去4年,因为我没有看到他们,一直是我生命中最幸福的几年

+0

添加一个if状态人员来检查所选行的索引 – user853710

4

提供导航的一种方法是使用BindingNavigator,您可以在其中删除不必要的按钮,而对于TextBox,您可以进行数据绑定。

负责加载数据的代码。如果您觉得合适,请将console.writeline更换为卡扣。写入日志文件等

public class DataOperations 
{ 
    public DataTable LoadCustomers() 
    { 
     DataTable dtCustomers = new DataTable(); 

     using (SqlConnection cn = new SqlConnection(Properties.Settings.Default.ConnectionString)) 
     { 
      string commandText = @"SELECT [Identfier], [CompanyName],[ContactName],[ContactTitle] FROM [NORTHWND1.MDF].[dbo].[Customers]"; 

      using (SqlCommand cmd = new SqlCommand(commandText, cn)) 
      { 
       try 
       { 
        cn.Open(); 
        dtCustomers.Load(cmd.ExecuteReader()); 
        dtCustomers.Columns["Identfier"].ColumnMapping = MappingType.Hidden; 
        dtCustomers.Columns["ContactTitle"].ColumnMapping = MappingType.Hidden; 
       } 
       catch (Exception ex) 
       { 
        Console.WriteLine(ex.Message); 
       } 
      } 
     } 

     return dtCustomers; 
    } 
} 

在一个形式中,一个BindingNavigator,一个的dataGridView,一个文本框

DataOperations dataOps = new DataOperations(); 
BindingSource bsCustomers = new BindingSource(); 
bsCustomers.DataSource = dataOps.LoadCustomers(); 
dataGridView1.DataSource = bsCustomers; 
bindingNavigator1.BindingSource = bsCustomers; 
txtContactTitle.DataBindings.Add("Text", bsCustomers, "ContactTitle"); 

到BindingNavigator一种替代是使BindingSource的形式水平,私有变量。然后在按钮中调用BindingSource.Move方法,例如bsCustomers.MoveFirst()。当然也有MoveNext,MoveLast和MovePrevious。

+0

尼斯干净的例子,+1 – OhBeWise

2
//first 

int i = 0; 
    this.dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[dataGridView1.CurrentCell.ColumnIndex]; 


//prev 

    int prev = dataGridView1.CurrentRow.Index - 1; 
      if (prev >= 0) 
      { 
       this.dataGridView1.CurrentCell = dataGridView1.Rows[prev].Cells[dataGridView1.CurrentCell.ColumnIndex]; 
       //MessageBox.Show(dataGridView1[0, dataGridView1.CurrentRow.Index].Value.ToString()); 
      } 


//next 
    int next = dataGridView1.CurrentRow.Index + 1; 
      if (next < dataGridView1.Rows.Count) 
      { 
       this.dataGridView1.CurrentCell = dataGridView1.Rows[next].Cells[dataGridView1.CurrentCell.ColumnIndex]; 
       //MessageBox.Show(dataGridView1[0, dataGridView1.CurrentRow.Index].Value.ToString()); 
      } 
//last 
    int i = dataGridView1.Rows.Count - 1; 
      if (i < dataGridView1.Rows.Count) 
      { 
       this.dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[dataGridView1.CurrentCell.ColumnIndex]; 
       //MessageBox.Show(dataGridView1[0, dataGridView1.CurrentRow.Index].Value.ToString()); 
      } 
相关问题