Populating Gridview by Dataset [Resolved]

Posted by Rasagna under ASP.NET on 12/21/2015 | Points: 10 | Views : 247 | Status : [Member] | Replies : 2
I have following problem. In my application I need a gridview with dynamic columns. I created a dataset and I filled it with a SqlClient.SqlDataAdapter.Fill Method. After setting the datasource of my Datagridview the result will be displayed. But the problem is i like to display value corresponding to checked or unchecked(like yes for checked and no for unchecked) but of course in my table displayed as checked or unchecked (how I stored them in the database-used bit datatype). How is it possible to change the columntype in my example to Text(display yes or no)instead of checkbox.
Thanks in advance.


Posted by: Sheonarayan on: 12/21/2015 [Administrator] HonoraryPlatinum | Points: 50


Below is the approach you can follow to solve your problem. This article of working with DataTable will help you in this regard http://www.dotnetfunda.com/articles/show/131/datatable-adding-modifying-deleting-filtering-sorting-rows-readingwrit.

Step 1 - Before setting the DataSource of the GridView, create a new DataTable like var myNewTable = new DataTable();

Step 2 - Add columns exactly the same as you have into your original DataTable EXCEPT the bit column where you have checked and unchecked column. Make this column as varchar where you are going to store Yes/No. Let's say this column name is "IsMale"

Step 3 - Loop through each rows of the original table and add new row into the myNewTable created into step 1. In each iteration of the loop while saving the value of last column "IsMale" created in step 2, check whether the bit column value of original table is true, if it is then store "Yes" into "IsMale" column otherwise store "No".

Step 4 - Now you have all the rows of the original DataTable to new DataTable that we have created in step 1 with Yes and No values instead of bit value (checked/unchecked).

Step 5 - Set the GridView data source to the new datatable (myNewTable) where your modified data is there

You should be getting Yes/No now in the gridview output.

Hope this will solve your problem, do not forget to read the above article that will help you log following above steps.


Sheo Narayan

Rasagna, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rasagna on: 12/21/2015 [Member] Starter | Points: 25

@Sheonarayan thanks for reply helped lot..

Rasagna, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response