Datagridview displaying incomplete id

Asked

Viewed 45 times

2

The grid displays only the first character of the "ID"

For example: if the ID is 16 it displays 1; If it’s 20 displays 2;

However from 1 to 9 displays correctly...

inserir a descrição da imagem aqui

Code that selects the data...

 private void lixeiraDialog_Load(object sender, EventArgs e)
        {
            //carrega do banco todos os registros excluidos (status = 0) 
            MySqlCommand sql = this.conexaoBanco.criaComandoSQL("sp_select_lixeira");
            sql.Parameters.Add(new MySqlParameter("nome_tabela", this.tabela));
            sql.Parameters.Add(new MySqlParameter("id_tabela", this.pk));
            sql.Parameters.Add(new MySqlParameter("campo_tabela", this.campo));
            //preenche o dataset
            MySqlDataAdapter mda = new MySqlDataAdapter(sql);
            mda.Fill(this.dadosTabela);

            //carrega o datagrid com os dados do dataset
            this.gridRegistrosDeletados.DataSource = this.dadosTabela.Tables[0];
            this.gridRegistrosDeletados.Columns[0].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
        }

Procedure:

CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_select_lixeira`(
    IN nome_tabela VARCHAR(255),
    IN id_tabela VARCHAR(10),
    IN campo_tabela VARCHAR(255)
)
    DETERMINISTIC
BEGIN
DECLARE maxDigitos varchar(11);
DECLARE comandoSQL VARCHAR(255);

-- pega o tamanho do campo codigo e atribui o valor na variavel maxDigitos --

SET @comandoSQL = CONCAT("SELECT (" ,id_tabela, ") FROM ", nome_tabela," LIMIT 1 INTO @maxDigitos");
PREPARE consulta FROM @comandoSQL;
EXECUTE consulta;
deallocate prepare consulta;

SET @comandoSQL = CONCAT("SELECT LPAD(",id_tabela,", ",@maxDigitos,", 0) AS ",id_tabela,", ", campo_tabela," AS descricao FROM ", nome_tabela," WHERE status = 0");

PREPARE consulta FROM @comandoSQL;
EXECUTE consulta;
deallocate prepare consulta;
END
  • for q the command with lpad with maxdigits ?! I just passed the eye, but that seems to be it

1 answer

2

CREATE DEFINER=`root
`@`localhost` PROCEDURE `sp_select_lixeira`(
    IN nome_tabela VARCHAR(255),
    IN id_tabela VARCHAR(10),
    IN campo_tabela VARCHAR(255)
)
    DETERMINISTIC
BEGIN
    DECLARE maxDigitos varchar(11);
    DECLARE comandoSQL VARCHAR(255);

    -- pega o tamanho do campo codigo e atribui o valor na variavel maxDigitos --

    SET @comandoSQL = CONCAT("SELECT (" ,id_tabela, ") FROM ", nome_tabela," LIMIT 1 INTO @maxDigitos");
    PREPARE consulta FROM @comandoSQL;
    EXECUTE consulta;
    deallocate prepare consulta;

    SET @comandoSQL = CONCAT("SELECT (",id_tabela," ) AS ",id_tabela,", ", campo_tabela," AS descricao FROM ", nome_tabela," WHERE status = 0");

    PREPARE consulta FROM @comandoSQL;
    EXECUTE consulta;
    deallocate prepare consulta;
END

It was a mistake in the trial... I made it wrong... Coffee shortage maybe

Browser other questions tagged

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