Create GridView Dynamically

I have a DataTable containing records. The table contains 2 columns - Questions and Type, so I want my GridView to have just these 2 columns. However,
If Type value of the Question is 1, I want to place a text box in Type column for that question.
If Type value of the Question is 2, I want to place a check box on so on.

For example, my grid should look similar to
Question1               Textbox
Question2               Checkbox
Question3               Checkbox
Question4               RadioButtonList  etc...

How could I do this dynamically in C#.  Thanks in advance..
Who is Participating?
Melih SARICAConnect With a Mentor IT ManagerCommented:
check the sample
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">
  void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
    if(e.Row.RowType == DataControlRowType.DataRow)
      // Display the company name in italics.
      e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
<html xmlns="" >
  <head runat="server">
    <title>GridView RowDataBound Example</title>
    <form id="form1" runat="server">
      <h3>GridView RowDataBound Example</h3>
      <asp:gridview id="CustomersGridView" 
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"

Open in new window

mmassyAuthor Commented:
Thanks man... below is some code to make the solution clear.

//Create Text Box
if (e.Row.Cells[1].Text = "1")
    TextBox txt = new TextBox()

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.