0
votes

I searched the web and Stack Overflow and found lots of descriptions on how to fill a DataGridView with the content of a DataTable. But still it does not work for me. My DataGridView shows the correct number of columns and rows, but they appear empty.

I use following method:

public void ShowDataInGrid(ref DataTable table)
    {
        BindingSource sBind = new BindingSource();
        dbView.Columns.Clear();
        dbView.AutoGenerateColumns = false;
        sBind.DataSource = table;
        dbView.DataSource = sBind;    //Add table to DataGridView
        dbView.Columns.Add("Date", "Date");            
    }

Before this I created a DataGridView of name "dbView" via the designer. I am not even sure, whether I need sBind. Without it I can bind the table directly to dbView, with the same bad result.

I suspect my table is the problem. It origins from a database (SQLite) and has several columns and rows (one of the columns has the name "Date"). It is definately filled with readable data. I mainly read the table in using following commands (after this I manipulate the data in several different steps, like changing strings and adding numbers...):

        string sql = "select * from Bank";
        SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
        SQLiteDataReader reader = command.ExecuteReader();
        table.Load(reader);
        reader.Close();
        table.AcceptChanges();

I think the problem might be, that the table entries are stored as objects and not as string, and hence can't be shown. That's why I tried to force the content to be strings with the following change to my table:

DataTable dbTableClone = new DataTable();
dbTableClone.Load(reader);
SQLiteDataReader reader.Close();
dbTableClone.AcceptChanges();

string[] dBHeader = new string[dbTableClone.Columns.Count];
dBHeader = ReadHeaderFromDataTable(dbTableClone); //own funktion, which reads the header
DataTable table;
table.Clear();
//will first create dbTable as empty clone, so I can set DataTyp of each Column
table = dbTableClone.Clone(); 
for (int col = 0; col > dBHeader.Length; col++) //first set all columns as string
{
   dbTable.Columns[col].DataType = typeof(string);
}
foreach (DataRow Row in dbTableClone.Rows)
{
   dbTable.ImportRow(Row);
}

This did not help me neither. Another idea: I found some comments on similar problems, where it got apparently solved with quote: "I designed columns in the VS datagridview designer. Not the column name, but the column DataPropertyName must match with fields in database." Unfortunately I don't seem to be able to do/understand this.

Following you see one row of my input table.

enter image description here

2
Can you define your problem in short and precise?M. Adeel Khalid
DataViewTable shows a table with the correct number of rows (same as bound table) but without content. Each Cell is empty.Birk
Have you seen that table has data after execution of the query?table.Load(reader);M. Adeel Khalid
Yes table certainly has data. Following command fills singleRowContent: singleRowContent[row] = Convert.ToString(table.Rows[row][0]); However I need the "Convert" otherwise I get an error: singleRowContent[row] = FDBHandler.dbable.Rows[row][0]; (cannot implicitly convert type object to string)Birk

2 Answers

0
votes

Try fetching and setting to GridView this way

        SqlLiteConnection con = new SqlLiteConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=DB.mdf;Integrated Security=True");
        con.Open();
        SqlLiteDataAdapter adap = new SqlLiteDataAdapter("select * from Bank", con);
        DataSet ds = new System.Data.DataSet();
        adap.Fill(ds);
        dataGridView1.DataSource = ds.Tables[0];

Comment everything you've done so far, try this and let me know if this works for you or not. Change connection according to your DB.

0
votes

I solved the problem. The DataTable was fine. The problem was the setup of my DataGridView dbView. I set up dbView in the designer and somehow gave it a datasource. Now I set the datasource to "none" (In "DataGridView Tasks") and my data appears as intended. Thanks to M Adeel Khalid for looking at my stuff. Him assuring to me that my code for the link was right, made me find the solution eventually. At the end I really only needed to use a single line:

dbView.DataSource = table;