C# 如何向datagridview添加上一个和下一个按钮

C# 如何向datagridview添加上一个和下一个按钮,c#,winforms,datagridview,C#,Winforms,Datagridview,我使用的是Visual studio 2012,并制作了一个windows窗体应用程序,对于其中一个窗体,我使用的是datagridview,它显示了SQL数据库中表的信息 我已经使表单自动从datagridview行直接加载信息到文本框中 SqlDataAdapter SDA = new SqlDataAdapter("SELECT * FROM Stock", con); DataTable DATA = new DataTable(); SDA.Fill(DATA); dataGridVi

我使用的是Visual studio 2012,并制作了一个windows窗体应用程序,对于其中一个窗体,我使用的是datagridview,它显示了SQL数据库中表的信息

我已经使表单自动从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();

问题是,我需要添加一个previous按钮和一个next按钮,以便用户可以在datagridview行中导航,并在文本框中查看datagridview行中每列的信息。我看过类似的问题,也浏览过互联网,寻找解决我问题的方法,但我似乎找不到一种适合我的代码的方法。您还可以告诉我如何添加一行代码,告诉用户如果单击数据库所有行的下一步,则没有更多行可供选择

将网格中的组件配置为匹配类型。然后在查询之后,将数据绑定到此gridView。添加两个按钮,其中“下一步”按钮将获取网格的currentselectedrow,并将其跟随者设置为所选的跟随者。上一个将起相反的作用。这是个小麻烦。我讨厌WinForms中的网格。过去的4年,因为我没有看到它们,是我一生中最快乐的几年

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

负责加载数据的代码。按照您认为合适的方式替换catch中的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和一个TextBox

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。

作为Karen解决方案的替代方案,如果您喜欢/必须使用按钮导航,那么您需要处理
CurrentCellChanged
事件以及以下按钮
单击
事件:

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];
}
private void DataGridView1\u CurrentCellChanged(对象发送方,事件参数e)
{
如果(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
如果可以单击“上一步”或“下一步”,则CurrentCellChanged事件将处理逻辑。它们各自的单击事件只是将当前单元格向后或向前移动一行。

//首先
//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());
            }
int i=0; this.dataGridView1.CurrentCell=dataGridView1.Rows[0]。单元格[dataGridView1.CurrentCell.ColumnIndex]; //上 int prev=dataGridView1.CurrentRow.Index-1; 如果(上一个>=0) { this.dataGridView1.CurrentCell=dataGridView1.Rows[prev].Cells[dataGridView1.CurrentCell.ColumnIndex]; //显示(dataGridView1[0,dataGridView1.CurrentRow.Index].Value.ToString()); } //下一个 int next=dataGridView1.CurrentRow.Index+1; if(next
Simple,你把它扔掉,然后在WPF和EFF中执行,这就是我要说的。使用WPFHave你至少尝试过谷歌搜索。看看Youtube。至少有100个视频向您展示了这种复杂程度,以及如何通过单击来实现这一点我已经看过了,但他们的做法是使用bindingsource,而我在代码中根本没有使用bindingsource。学习它的绝佳机会。BindingSources是一个不好的概念,但是您的问题显示了您需要添加一个if语句来检查所选行的索引的复杂程度。很好的例子,+1谢谢,如果没有更多行,我本来打算通过显示一个messagebox来做这件事,但我认为您的方式要好得多。谢谢你,因为你已经解决了我的问题,谢谢你对Karen Payne和user853710的回答,这也是解决问题的有效方法。