使用按钮滚动DataGridView

时间:2016-05-06 08:06:28

标签: c# datagridview

您好我有一个代码,我在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

< -------------------------- EDITED ------------------ --------------->

我尝试过这样的事情: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行?

1 个答案:

答案 0 :(得分:0)

我使用此面板而不是数据网格解决了问题,因为你想要的代码将会更好:

方法:

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);
    }

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