Solved

Create GridView Dynamically

Posted on 2008-06-13
2
2,727 Views
Last Modified: 2013-12-17
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..
0
Comment
Question by:mmassy
2 Comments
 
LVL 19

Accepted Solution

by:
Melih SARICA earned 500 total points
Comment Utility
check the sample
<%@ Page language="C#" %>
 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<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>";

        

    }

    

  }
 

</script>
 

<html xmlns="http://www.w3.org/1999/xhtml" >

  <head runat="server">

    <title>GridView RowDataBound Example</title>

</head>

<body>

    <form id="form1" runat="server">

        

      <h3>GridView RowDataBound Example</h3>
 

      <asp:gridview id="CustomersGridView" 

        datasourceid="CustomersSqlDataSource" 

        autogeneratecolumns="true"

        allowpaging="true"

        onrowdatabound="CustomersGridView_RowDataBound" 

        runat="server">

      </asp:gridview>

            

      <!-- 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%>"

        runat="server">

      </asp:sqldatasource>

            

            

    </form>

  </body>

</html>

Open in new window

0
 

Author Comment

by:mmassy
Comment Utility
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()

    e.Row.Cells[1].Controls.Add(txt);

} 

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now