Error while trying to add an auto-incrementable column to a datagridview

Asked

Viewed 131 times

0

I’m trying to add a column self-adjusting in a datagridview but I am not able to figure out what is missing to work properly. My program reads the data from a database table and displays in a datagridview. I need to add a column self-adjusting called 'Legend', because the table values are also represented in a graph and the column 'Legend' exists to facilitate the interpretation of the data by the user.

Follows part of the code responsible for this. The column called 'Caption' appears in datagridview, but the lines are blank.

Conexao ca = new Conexao();
string sql = "";
sql += " Select ";
sql += " CausaDef, Incidencia ";
sql += " From ";
sql += " Relat ";

ca.Conectar();
OleDbDataAdapter da = new OleDbDataAdapter(sql, ca.cx);
DataSet ds = new DataSet();
da.Fill(ds, "DetGraf");

object sumObject;
sumObject = ds.Tables["DetGraf"].Compute("Sum(Incidencia)", "");

ds.Tables["DetGraf"].Rows.Add("TOTAL", sumObject.ToString());

DataColumn Legenda = new DataColumn();
Legenda.ColumnName = "Legenda";
Legenda.DataType = typeof(int);
Legenda.AutoIncrement = true;
Legenda.AutoIncrementSeed = 1;
Legenda.AutoIncrementStep = 1;

ds.Tables["DetGraf"].Columns.Add(Legenda);

dgvDetGraf.DataSource = ds.Tables["DetGraf"];

ca.Desconectar();
  • Put the total separate from the Main Grid and you cannot mix Caption with the data coming from the database. what you want to show put a picture of what you want to implement, I’ve seen two errors in your code I just won’t post an answer because I got more doubt kkk !

2 answers

0


I was able to find a solution as follows:

ds.Tables["DetGraf"].Columns.Add(Legenda);

int index = 0;
foreach (DataRow row in ds.Tables["DetGraf"].Rows)
row.SetField(Legenda, ++index);

dgvDetGraf.DataSource = ds.Tables["DetGraf"];

Abs!

0

It tries to modify the code, instead of creating this "auto-increment" column, it tries to bring the number of the database row, through the ROW_NUMBER function that if it is SQL SERVER. So just apply the data source to the datagrid.

Conexao ca = new Conexao();
string sql = "";
sql += " Select ";
sql += " ROW_NUMBER() OVER(ORDER BY CausaDef) AS Legenda,";
sql += " CausaDef, Incidencia ";
sql += " From ";
sql += " Relat ";

ca.Conectar();
OleDbDataAdapter da = new OleDbDataAdapter(sql, ca.cx);
DataSet ds = new DataSet();
da.Fill(ds, "DetGraf");

object sumObject;
sumObject = ds.Tables["DetGraf"].Compute("Sum(Incidencia)", "");

ds.Tables["DetGraf"].Rows.Add("TOTAL", sumObject.ToString());

dgvDetGraf.DataSource = ds.Tables["DetGraf"];

ca.Desconectar();
  • Hello, Murilo! Thank you for the reply. Unfortunately my database is Access.

  • your table does not have a primary auto-increment key that would serve as the "legend column"?

  • Yes, there is a primary key. The problem is that the table is deleted and filled several times. The key is already in the range of over 2000... rs I believe this key cannot be reset :(

Browser other questions tagged

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