none
Formatting cells in DGV, values sourced from DataTable RRS feed

  • Question

  • I tried displaying values in a DGV (6 columns) directly from multiple tables (SqlDataReader), but its proving cumbersome.  Adding one or more columns to a DGV automatically generates a row.  I can add values into row 0, but if there are multiple rows in the query result, I have to add another row to facilitate the next result or I get out of range exception with row index.  Problem is, SqlDataReader has no property to tell if there are any more results, no end of file equivalent.  So I tried using an intermediary, DataTable, which doesn't create a row after adding column(s).  I've managed to add two rows, and display values from a table in columns 1 and 2.  I also want to style the values, however, DataTable has no formatting.  So how do I or can I style the values in the DGV after sourcing them from the DataTable?
    Saturday, February 17, 2018 1:00 AM

Answers

  • Success.  I initiated a count = 0 before reading results.  After the first read, I check to see if cell 0 in the last row is empty(""), if yes, add a row and increase the count ++.  After the last read, I check if cell 0 in the last row is null, if yes delete the row.  

    Saturday, February 17, 2018 8:04 PM

All replies

  • Hello,

    Since you have no mention of what you have tried I would suggest looking at subscribing to CellFormatting event. The cell formatting event has no ties to how the data is loaded. 

    I'm unclear why it's important to focus on row 0 and so on as this has nothing to do with formatting as the CellFormatting event formats regardless.

    Separate data from formatting as one should load a DataGridView by it's DataSource by it loading a DataTable in one shot and later add rows as needed or perhaps using a iterator method to populate the DataGridView which has a DataSource of a DataTable or even a list of a concrete class.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, February 17, 2018 10:28 AM
    Moderator
  • I create the DGV, added columns and formatted them.  I also created the DataTable and added values to it.  I set the DataTable as the data source for the DGV.  Nothing displayed in the DGV.  I commented out the columns for the DGV, the values from the DataTable displayed in the DGV.  
    Saturday, February 17, 2018 2:34 PM
  • After reading from a database SqlDataReader has x rows, x is unknown.  After adding columns to the DGV, row 0 is created automatically.  If there is only one row in the result, there is no need to add more rows.  Before inserting values I am checking to see if there is anything in the first column of the last row. 

    While(Reader.Read())
    if(datagridview.Rows[datagridview.Rows.count-1].Cells["ColumnName"].value != "")
    {
    datagridview.Rows.Add();
    }
    When I run the program I'm getting exception System.Windows.Forms.DataGridViewCell.Value.get returned null.  

     
    Saturday, February 17, 2018 5:38 PM
  • Success.  I initiated a count = 0 before reading results.  After the first read, I check to see if cell 0 in the last row is empty(""), if yes, add a row and increase the count ++.  After the last read, I check if cell 0 in the last row is null, if yes delete the row.  

    Saturday, February 17, 2018 8:04 PM