DataGrid boundcolumn problem

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

I want to use a datagrid with my display, but I don't want to use 1 row per
record.

What I have is a record with up to 6 values -
(answer1,answer2,answer3,answer4,answer5,answer6). I want to display them
with alternating styles. One record may only have the first 3 fields with
data and another with 4 and still another with 6.

I want it to show as:

Answer1
Answer2
Answer3

or

Answer1
Answer2
Answer3
Answer4
Answer5

Can I set up a DataGrid this way?

Thanks,

Tom
 
If you normalize your database you will get that display. Something like
this:

Questions Answers
1
2
3
4
etc.
 
Hi,

Is your req. is like this,

No. Data

1 Answer1
Answer2
Answer3

2 Answer1
Answer2
Answer3
Answer4

Regards,
Kamal T.
 
Tshad,

Assuming that you want them showed horizontal, you can do it by creating
extra columns with expressions, when you than shows those columns using
styles it should be very easy in my opinion with a windowforms datagrid.

I hope this helps?

Cor
 
Earl said:
If you normalize your database you will get that display. Something like
this:

My table is set like this:

CREATE TABLE [dbo].[ScreenQuestions] (
[PositionID] [int] NOT NULL ,
[Unique] [int] NOT NULL ,
[Question] [text] NULL ,
[QuestionType] [char] (10) NULL ,
[Answer1] [text] NULL ,
[Answer2] [text] NULL ,
[Answer3] [text] NULL ,
[Answer4] [text] NULL ,
[Answer5] [text] NULL ,
[Answer6] [text] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

I could normalize the table and put the answers in another table (1 record
per answer), but I will never have more than 6 and more likely 4 or 5.

It just seemed more complicated and one more join for something I can get in
one record read.
 
Kamal T. said:
Hi,

Is your req. is like this,

No. Data

1 Answer1
Answer2
Answer3

2 Answer1
Answer2
Answer3
Answer4

As I added in my last post, my table is:

CREATE TABLE [dbo].[ScreenQuestions] (
[PositionID] [int] NOT NULL ,
[Unique] [int] NOT NULL ,
[Question] [text] NULL ,
[QuestionType] [char] (10) NULL ,
[Answer1] [text] NULL ,
[Answer2] [text] NULL ,
[Answer3] [text] NULL ,
[Answer4] [text] NULL ,
[Answer5] [text] NULL ,
[Answer6] [text] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

What I want to do is set up a datalist that display all the questions., then
if the user pushes a button, it will display the answers below the question.
Something like:

+ Question 1
+ Question 2
+ Question 3

Then like a tree, if you push the "+" on a question, the answers will show
below in a grid, something like:

+ Question 1
- Question2
Answer1
Answer2
Answer3
Answer4
Answer5
+ Question3

Tom
 
Hi,

It is possible to do it in DataGrid. I have implemented it, in my sample
code. I have pasted my sample code below:

Table Script
~~~~~~~~~

CREATE TABLE ModelExam (QuestionID IDENTITY INT,Question VARCHAR(50),Answer1
VARCHAR(50),Answer2 VARCHAR(50),Answer3 VARCHAR(50))

Set QuestionID -> Primary Key

Web.config configuration
~~~~~~~~~~~~~~~~

<appSettings>
<add key="ConnectionString" value="Data Source=VIJAY\NETSDK;Integrated
Security=SSPI;Initial Catalog=Northwind;"></add>
</appSettings>

Add the above mentioned key in web.config inside the <configuration> tag

ModelExam.aspx file
~~~~~~~~~~~~~

<%@ Page language="c#" Codebehind="ModelExam.aspx.cs"
AutoEventWireup="false" Inherits="testCompositeControl.ModelExam" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">

<script>
function ToggleAnswers(objDiv,objButton)
{
if (objDiv && objButton)
{
if (objDiv.style.display == 'none')
{
objDiv.style.display = '';
objButton.value = "-";
}
else if (objDiv.style.display == '')
{
objDiv.style.display = 'none';
objButton.value = "+";
}
}
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="frmModelExam" method="post" runat="server">
<asp:DataGrid id="dgExamInfo" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn ItemStyle-VerticalAlign=Top>
<ItemTemplate>
<input type=button value="+" id="btnQuestionID" runat=server
onclick=<%# "ToggleAnswers(div" +
DataBinder.Eval(Container.DataItem,"QuestionID") + ",this);"%> >
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Label ID="lblQuestion" Runat="server">
<%# DataBinder.Eval(Container.DataItem,"Question") %>
</asp:Label>
<asp:DataList ID="dlstModelExam" Runat=server DataSource=<%#
GetAnswers(DataBinder.Eval(Container.DataItem,"QuestionID"))%> >
<ItemTemplate>
<div style="display:none" id=<%# "div" +
DataBinder.Eval(Container.DataItem,"QuestionID")%>>
<table>
<tr><td><%# DataBinder.Eval(Container.DataItem,"Answer1")
%></td></tr>
<tr><td><%# DataBinder.Eval(Container.DataItem,"Answer2")
%></td></tr>
<tr><td><%# DataBinder.Eval(Container.DataItem,"Answer3")
%></td></tr>
</table>
</div>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</HTML>


ModelExam.aspx.cs file
~~~~~~~~~~~~~~~

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.SqlClient;

namespace testCompositeControl
{
/// <summary>
/// Summary description for ModelExam.
/// </summary>
public class ModelExam : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgExamInfo;
protected static string ConnectionString;

/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)

ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];
SqlDataAdapter sqlDataAdp = new SqlDataAdapter(@"SELECT
QuestionID,Question,ISNULL(Answer1,'') AS Answer1,ISNULL(Answer2,'') AS
Answer2,ISNULL(Answer3,'') AS Answer3
FROM ModelExam",new SqlConnection(ConnectionString));

DataSet dsExamInfo = new DataSet();
sqlDataAdp.Fill(dsExamInfo,"ExamInfo");

Cache["ModelExam"] = dsExamInfo;

DataView dView = new DataView(dsExamInfo.Tables[0]);
dgExamInfo.DataSource = dView;
dgExamInfo.DataBind();
}
}

/// <summary>
/// This method is triggered for each QuestionID
/// </summary>
/// <param name="questionID"></param>
/// <returns></returns>
public DataSet GetAnswers(object questionID)
{
DataSet dsResult = (DataSet)Cache["ModelExam"];

DataRow[] drRowList = dsResult.Tables[0].Select("QuestionID = " +
questionID.ToString());

DataSet retResult = new DataSet();
retResult = dsResult.Clone();

foreach(DataRow drRow in drRowList)
{
retResult.Tables[0].ImportRow(drRow);
}
return retResult;
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}
 
Back
Top