【问题标题】:Scrolling DataGridView with buttons使用按钮滚动 DataGridView
【发布时间】:2016-05-06 08:06:27
【问题描述】:

您好,我有一个代码,我在 datagridview 内部使用来自 sql server 数据库的数据生成按钮。但现在我想通过按钮滚动它们。我尝试了很多事情都给了我错误已经看到了一个关于这个的帖子但是没有任何对我有用的人可以帮助。

填充datagridview的方法:

public void TabelaFuncionario()
{
    try
    {
        BDfuncionarios = new DataTable();
        string cmd = "My select string";
        var adpt = fConexao.GetDataAdapter(cmd);
        BDfuncionarios.Clear();
        adpt.Fill(BDfuncionarios);
    }
    catch (Exception r)
    {
        MessageBox.Show(r.Message);
    }
}

public void BotaoFuncionario()
{
    try
    {
        TabelaFuncionario();
        PosXartigo = 1;
        PosYartigo = 1;

        //Apagar o painel todo
        dataGridView1.Controls.Clear();
        foreach (DataRow row in BDfuncionarios.Rows)
        {
            int posicaoX = ((PosXartigo - 1) * Gap_Xartigo) + xInicial + (Largura_BotaoArtigo * (PosXartigo - 1));
            if (posicaoX > maximoxArtigo)
            {
                PosYartigo++; PosXartigo = 1;
            }
            else
            {
                PosXartigo = PosXartigo != 1 ? PosXartigo++ : 1;
            }
            int PontoX = ((PosXartigo - 1) * Gap_Xartigo) + xInicial + (Largura_BotaoArtigo * (PosXartigo - 1));
            int PontoY = ((PosYartigo - 1) * Gap_Yartigo) + yInicial + (Altura_BotaoArtigo * (PosYartigo - 1));
            Button bt1 = new Button();
            bt1.Location = new Point(PontoX, PontoY);
            Mo mo = new Mo();
            mo.codmo = (int)row["Something"];
            mo.nome_func = (string)row["Something"];
            bt1.Name = "Botao" + NBotoes.ToString();
            bt1.Height = Altura_BotaoArtigo;
            bt1.Width = Largura_BotaoArtigo;
            bt1.BackColor = Color.Tan;
            bt1.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold);
            bt1.ForeColor = Color.Black;
            bt1.Text = mo.nome_func;
            bt1.Tag = mo;
            bt1.FlatStyle = FlatStyle.Popup;
            bt1.Click += btArtigo_click;
            dataGridView1.Controls.Add(bt1);

            NBotoes++;
            PosXartigo++;
        }
    }
    catch (Exception r)
    {
        MessageBox.Show(r.Message);
    }
}

我的表单图片(不知道是否有帮助):

http://imgur.com/f5G25nX

我尝试过这样的事情:https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowcount(v=vs.110).aspx

让我超出范围或类似的东西

刚刚试了一下

int row = dataGridView1.RowCount;
MessageBox.Show(row+"");

它显示我 0;如何在网格中有按钮但有 0 行?

【问题讨论】:

  • stackoverflow.com/questions/19648872/… 类似问题 - 涉及滚动到特定行
  • all 给我错误。您遇到了什么错误,您尝试过什么?
  • @NullException 我编辑了问题
  • 您向Controls 集合添加了一个按钮。它不适合DataGridView 中的单元格。你试过DataGridViewButtonColumn吗?

标签: c# datagridview


【解决方案1】:

我使用这个面板而不是 datagrid 解决了这个问题,因为你想要它更好,代码如下:

方法:

public void TabelaFuncionario()
    {

        try
        {
            BDfuncionarios = new DataTable();
            string cmd = "your select";
            var adpt = fConexao.GetDataAdapter(cmd);
            BDfuncionarios.Clear();
            adpt.Fill(BDfuncionarios);
        }
        catch (Exception r)
        {

            MessageBox.Show(r.Message);
        }
    }

    public void BotaoFuncionario()
    {
        try
        {

            TabelaFuncionario();
            PosXartigo = 1;
            PosYartigo = 1;

            //Apagar o painel todo
            panel2.Controls.Clear();
            foreach (DataRow row in BDfuncionarios.Rows)
            {
                int posicaoX = ((PosXartigo - 1) * Gap_Xartigo) + xInicial + (Largura_BotaoArtigo * (PosXartigo - 1));
                if (posicaoX > maximoxArtigo)
                {
                    PosYartigo++; PosXartigo = 1;
                }
                else
                {
                    PosXartigo = PosXartigo != 1 ? PosXartigo++ : 1;
                }
                int PontoX = ((PosXartigo - 1) * Gap_Xartigo) + xInicial + (Largura_BotaoArtigo * (PosXartigo - 1));
                int PontoY = ((PosYartigo - 1) * Gap_Yartigo) + yInicial + (Altura_BotaoArtigo * (PosYartigo - 1));
                Button bt1 = new Button();
                bt1.Location = new Point(PontoX, PontoY);
                Mo mo = new Mo();
                mo.codmo = (int)row["Var1"];
                mo.nome_func = (string)row["Var2"];

                bt1.Name = "Botao" + NBotoes.ToString();
                bt1.Height = Altura_BotaoArtigo;
                bt1.Width = Largura_BotaoArtigo;
                bt1.BackColor = Color.Tan;
                bt1.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold);
                bt1.ForeColor = Color.Black;
                bt1.Text = mo.nome_func;
                bt1.Tag = mo;
                bt1.FlatStyle = FlatStyle.Popup;
                bt1.Click += btFuncionario_click;
                panel2.Controls.Add(bt1);

                NBotoes++;
                PosXartigo++;

            }
        }
        catch (Exception r)
        {

            MessageBox.Show(r.Message);

        }
    }

现在是 PainelExtension 类:

public static class PanelExtension
{
    public static void ScrollDown(this Panel p, int pos)
    {
        //pos passed in should be positive
        using (Control c = new Control() { Parent = p, Height = 1, Top = p.ClientSize.Height + pos })
        {
            p.ScrollControlIntoView(c);
        }
    }
    public static void ScrollUp(this Panel p, int pos)
    {
        //pos passed in should be negative
        using (Control c = new Control() { Parent = p, Height = 1, Top = pos })
        {
            p.ScrollControlIntoView(c);
        }
    }
}

上下按钮点击:

private void upbt_Click(object sender, EventArgs e)
    {
        if (i >= 0) i = -1;
        panel2.ScrollUp(i=i-30);
    }

    private void downbt_Click(object sender, EventArgs e)
    {
        if (i < 0) i = 0;
        panel2.ScrollDown(i=i+20);
    }

我让它像这样工作,也许还有其他方法可以做到这一点,我选择了这个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多