Solved

Create GridView Dynamically

Posted on 2008-06-13
2
2,739 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
ID: 21781940
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
ID: 21782276
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 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

18 Experts available now in Live!

Get 1:1 Help Now