【问题标题】:How do i add a previous and next button to datagridview如何将上一个和下一个按钮添加到 datagridview
【发布时间】:2016-02-01 22:08:32
【问题描述】:

我正在使用 Visual Studio 2012 并制作了一个 Windows 窗体应用程序,其中一个窗体我使用了一个显示 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 行的每一列中查看文本框中的信息。我查看了类似的问题并浏览了互联网以寻找解决我的问题的方法,但我似乎找不到适用于我的代码的方法。您还可以告诉我如何添加一行代码,告诉用户如果他们单击数据库的所有行中的下一步,则没有更多行可供选择。

【问题讨论】:

  • 简单,你把它扔掉,然后在 WPF 和 EF 中做
  • 这就是我要说的。使用 WPF
  • 你至少尝试过谷歌搜索。看看优酷。至少有 100 个视频向您展示了这种复杂程度以及如何通过点击来实现这一目标
  • 我看过了,但他们的做法是使用绑定源,我的代码中根本没有使用过。
  • 学习它的好机会。 BindingSources 是一个糟糕的概念,但您的问题表明您需要什么样的复杂性

标签: c# winforms datagridview


【解决方案1】:

提供导航的一种方法是使用 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。

【讨论】:

  • 漂亮干净的例子,+1
【解决方案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());
            }

【讨论】:

    【解决方案3】:

    作为 Karen 解决方案的替代方案,如果您更喜欢/必须使用按钮进行导航,那么您将需要处理 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 事件将处理您是否可以单击 PreviousNext 的逻辑。它们各自的点击事件只是将当前单元格向后或向前移动一行。

    【讨论】:

    • 谢谢,如果没有更多的行,我最初打算通过显示一个消息框来稍微不同,但我认为你的方式要好得多。谢谢 OhBeWise,您解决了我的问题,感谢 Karen Payne 和 user853710 的回答,这也是解决问题的有效方法。
    【解决方案4】:

    您将网格中的 comulmns 配置为您的匹配类型。然后在查询之后将数据绑定到这个 gridView。您添加两个按钮,其中“下一步”按钮将获取网格的当前选定行,并将其跟随者设置为选定的。以前会做相反的事情。这是一个小小的痛苦。我讨厌 WinForms 中的网格。过去的四年,因为我没有看到他们,是我一生中最快乐的几年

    【讨论】:

    • 添加一个 if 语句来检查所选行的索引
    猜你喜欢
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 2012-08-01
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    相关资源
    最近更新 更多