Windowsform - How to add two Datagridviewbuttoncell in the same Ell?

Asked

Viewed 96 times

0

Is it possible to add two Datagridviewbuttoncell type buttons in the same cell in a Datagridview? I’ve done a lot of research and so far I haven’t been able to find any solution.

Edit 1: For example, I wanted to add a [Morning] and another [Afternoon] button on the same... I tried to add a panel, but it does not accept, because Datagridviewbuttoncell is not Control type.

Edit 2: That’s what I can do these days...

inserir a descrição da imagem aqui

Edit 3: Well, I’ve come up with a solution for this business model. @Rovannlinhalis came up with the idea of creating an extra column so that it could divide between MORNING and AFTERNOON. The problem arises when the user chooses the BIWEEKLY or MONTHLY calendar option, as many columns would be generated... Well, instead of adding n columns, I added extra rows, which are divided between MORNING and AFTERNOON. Ex: inserir a descrição da imagem aqui

But I’m having some difficulties regarding the events of Windows Form. In this Datagridview I am creating a Rectangle and giving a Drawstring in it in Rowheader, for each line, where the room numbers appear (ROOM: 22). This is the solution I found because Datagridview does not have Rowspan.

Anyway, the problem appears when I select a line and then I select another one. It seems that Rectangle is invisible... Ex: inserir a descrição da imagem aqui

Event code:

reservaGrid_Grid.CellPainting += (sender, e) =>
        {
            if (e.ColumnIndex == -1 && e.RowIndex > -1)
            {
                //exibe as datas em cima das colunas, desenhando de duas em duas linhas
                for (int i = 0; i < reservaGrid_Grid.RowCount; i += 2)
                {
                    string quarto = string.Format( "Quarto: {0}", Funcoes.String.FindStringAfterCharacter(reservaGrid_Grid.Rows[i].Tag.ToString(), '_') );

                    Rectangle retangulo     = reservaGrid_Grid.GetCellDisplayRectangle(-1, i, false);
                    //int largura           = reservaGrid_Grid.GetCellDisplayRectangle(-1, i + 1, true).Width;
                    retangulo.X             += 1;
                    retangulo.Y             += 1;
                    retangulo.Width         = retangulo.Width / 2 + 15;//largura do header
                    retangulo.Height        = retangulo.Height;//tamanho do header, preenche 2 linhas
                    StringFormat format     = new StringFormat();
                    format.Alignment        = StringAlignment.Center;
                    format.LineAlignment    = StringAlignment.Center;

                    //pinta o fundo do retangulo
                    e.Graphics.FillRectangle(
                        new SolidBrush(
                            reservaGrid_Grid.ColumnHeadersDefaultCellStyle.BackColor
                        ),
                        retangulo
                    );

                    //desenha a string no retangulo
                    e.Graphics.DrawString(
                        quarto,
                        new Font(reservaGrid_Grid.ColumnHeadersDefaultCellStyle.Font.FontFamily, 13, FontStyle.Bold),
                        new SolidBrush(reservaGrid_Grid.ColumnHeadersDefaultCellStyle.ForeColor),
                        retangulo,
                        format
                    );
                }
            }
            //reservaGrid_Grid.Invalidate();
        };

Why will it be?

  • 2

    You will have to create a custom column that has two buttons.

  • Linq, look at my edited. I didn’t quite understand your reply, but thanks anyway xD

  • 2

    My comment remains the same: instead of using an object of the type DataGridViewButtonCell to be Datagrid cells, you will need to create a type that contains two buttons. Then you will need to implement all the methods needed for a cell and change the Datagrid to use this type instead of the DataGridViewButtonCell

  • 1

    I find it easier to duplicate the column, or manipulate the event of cell to display a control when clicked.

  • @Rovannlinhalis I had already thought of duplicating the column, but I’m afraid I have to change a lot. The bid of this grid would be to tell the user whether the room is occupied or not, painting the buttons or otherwise warning, and if not, allow the same to be reserved by clicking the button desired.

  • 1

    you can use an image, and vary by the images whether it is free or not... but thinking of having buttons, and for each, a different command, I believe that the simplest way would be to duplicate the same columns

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.