Number lines in Datagridview in C#

Asked

Viewed 69 times

-1

I have a DataGridView and a method to fill it:

public DataTable BuscarClientesEmAtividade()
{
    OleDbDataAdapter da;
    DataTable dt = new DataTable();

    OleDbConnection con = ConexaoBanco();
    OleDbCommand cmd = con.CreateCommand();
    cmd.CommandText = "SELECT codcli, nomecli FROM TBCLI WHERE ematividadecli = true";
    da = new OleDbDataAdapter(cmd.CommandText, con);
    da.Fill(dt);
    con.Close();
    return dt; 
}

My doubt is how to get it inserted in the first column of DataTable dt a numbering for the lines of the DataGridView?

  • Do you want to number lines from 1 to the general amount? a reminder if you are ACCESS by the look and are using Dataadapter it is kind of slow ...

1 answer

0

My friends is solved, I added this excerpt of code before filling the Datatable by the dataadapter, if later does not work right:

        DataColumn column = new DataColumn();

        // configurar varios atributos
        column.ColumnName = "Número";
        column.DataType = System.Type.GetType("System.Int32");
        column.AutoIncrement = true;
        column.AutoIncrementSeed = 1;
        column.AutoIncrementStep = 1;
        column.ReadOnly = true;

It works by creating the column then assigns integer value with 1-unit increment for each row whether it is added what will be done by the Dataadapter with database data. I hope this question can help other people with the same doubts as mine :).

Browser other questions tagged

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