473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

GridView generated programmatically

<asp:GridView ID="Basic" runat="server"
DataKeyNames="QuestionID,isHeading" AutoGenerateColumns="false"
AllowPaging="true" PageSize="100">
<Columns>
<asp:BoundField Visible="false" DataField="QuestionID"
HeaderText="QuestionID"></asp:BoundField>
<asp:BoundField DataField="QuestionDesc"
HeaderText="Question"
ItemStyle-HorizontalAlign="Left"></
asp:BoundField>
<asp:TemplateField HeaderText="Answer">
<ItemTemplate>
<asp:RadioButtonList
RepeatDirection="Horizontal" runat="server"
ID="rdAnswer" SelectedIndex='<%#
SetState(DataBinder.Eval
(Container.DataItem,"Answer"))%>'>
<asp:ListItem Value="0">Yes</
asp:ListItem>
<asp:ListItem Value="1">No</
asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Hello,

I need to create a gridview programmatically in code that would be
similar to the above code (in design mode).\

Any pointers?

Thanks,

Mar 28 '07 #1
5 23592
Someones already done it for you.
http://forums.asp.net/thread/1379019.aspx

Not sure why you wouldn't just use a templated gridview though!

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog
"NKaufman" <na****@hotmail.comwrote in message
news:11*********************@o5g2000hsb.googlegrou ps.com...
<asp:GridView ID="Basic" runat="server"
DataKeyNames="QuestionID,isHeading" AutoGenerateColumns="false"
AllowPaging="true" PageSize="100">
<Columns>
<asp:BoundField Visible="false" DataField="QuestionID"
HeaderText="QuestionID"></asp:BoundField>
<asp:BoundField DataField="QuestionDesc"
HeaderText="Question"
ItemStyle-HorizontalAlign="Left"></
asp:BoundField>
<asp:TemplateField HeaderText="Answer">
<ItemTemplate>
<asp:RadioButtonList
RepeatDirection="Horizontal" runat="server"
ID="rdAnswer" SelectedIndex='<%#
SetState(DataBinder.Eval
(Container.DataItem,"Answer"))%>'>
<asp:ListItem Value="0">Yes</
asp:ListItem>
<asp:ListItem Value="1">No</
asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Hello,

I need to create a gridview programmatically in code that would be
similar to the above code (in design mode).\

Any pointers?

Thanks,

Mar 28 '07 #2
Hello John,

Following are some of the issues in the example you cited:
(1) All columns of datatable will be displayed. In my case, I need
some columns to be visible and some are not.
(2) Does not handle DataKeys that I need
(3) Does not handle adding a control like the radiobuttonlist that I
need

In a post from the same thread - "why not a basic gridview? .....
since one your example you are not doing additional formatting to the
data other than creating bound columns."

Thanks for you assistance.
On Mar 28, 4:09 pm, "John Timney \(MVP\)"
<x_j...@timney.eclipse.co.ukwrote:
Someones already done it for you.http://forums.asp.net/thread/1379019.aspx

Not sure why you wouldn't just use a templated gridview though!

Regards

John Timney (MVP)http://www.johntimney.comhttp://www.johntimney.com/blog
Mar 28 '07 #3
I didn't think the example gave you a perfect solution, only that it would
give you an idea of how to create a datagrid dynamically.

If you use a template against a gridview then you can BIND and EVAL against
any of the datatable values and structure your output as you need it,

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog
"NKaufman" <na****@hotmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
Hello John,

Following are some of the issues in the example you cited:
(1) All columns of datatable will be displayed. In my case, I need
some columns to be visible and some are not.
(2) Does not handle DataKeys that I need
(3) Does not handle adding a control like the radiobuttonlist that I
need

In a post from the same thread - "why not a basic gridview? .....
since one your example you are not doing additional formatting to the
data other than creating bound columns."

Thanks for you assistance.
On Mar 28, 4:09 pm, "John Timney \(MVP\)"
<x_j...@timney.eclipse.co.ukwrote:
>Someones already done it for
you.http://forums.asp.net/thread/1379019.aspx

Not sure why you wouldn't just use a templated gridview though!

Regards

John Timney (MVP)http://www.johntimney.comhttp://www.johntimney.com/blog

Mar 28 '07 #4

Hi,

To generate a gridView programmatically, you need to create a GridView
object and assign the properties that you had mentioned.
BoundField objects can be created and added to the gridview object.
Problem comes only when you need to add an ItemTemplate column. I tried a
sample code for the exact same gridView code that you had posted.

//My sample datatable which i am using to bind to the grid view.
DataTable dtSource = new DataTable();
dtSource.Columns.Add("QuestionID");
dtSource.Columns.Add("QuestionDesc");
dtSource.Columns.Add("Answer");
dtSource.Columns.Add("isHeading");

DataRow dr = dtSource.NewRow();
dr[0] = "Q1";
dr[1] = "I like reading books ";
dr[2] = "1";
dr[3] = "true";
dtSource.Rows.Add(dr);

dr = dtSource.NewRow();
dr[0] = "Q2";
dr[1] = "I watch a lot of TV";
dr[2] = "0";
dr[3] = "false";
dtSource.Rows.Add(dr);
//Creating a gridView

GridView gvBasic = new GridView();

gvBasic.DataKeyNames="QuestionID,isHeading".Split( ",".ToCharArray());
gvBasic.AutoGenerateColumns = false;
gvBasic.AllowPaging = true;
gvBasic.PageSize = 100;
gvBasic.DataSource = dtSource;

//Your column 1 that is hidden

BoundField objCol1 = new BoundField();
objCol1.Visible= false;
objCol1.DataField = "QuestionID";
objCol1.HeaderText = "QuestionID";
gvBasic.Columns.Add(objCol1);

//Your column 2 that has the questions

BoundField objCol2 = new BoundField();
objCol2.DataField = "QuestionDesc";
objCol2.HeaderText = "Question";
objCol2.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
gvBasic.Columns.Add(objCol2);

//ItemTemplate is an Interface in dotnet and hence,
//Create an ItemTemplate class which implements the interface.

// Your column three which has the radiobuttonList on it

TemplateField objCol3 = new TemplateField();
objCol3.HeaderText = "Answer";
ItemTemplate objTemplate = new ItemTemplate("rdAnswer", dtSource);
objCol3.ItemTemplate = objTemplate;

gvBasic.Columns.Add(objCol3);

gvBasic.DataBind();
Page.Form.Controls.Add(gvBasic);

Code for the ItemTemplate class:
public class ItemTemplate : ITemplate
{

string ControlName = "";
object DataSource = null;

//ControlName: Name of the radio button list.
// DataSource is the datatable to which the gridview is bound to.

public ItemTemplate(string ControlName,object DataSource)
{
this.ControlName = ControlName;
this.DataSource = DataSource;
}
public void InstantiateIn(Control objContainer)
{
RadioButtonList rdAnswer = new RadioButtonList();
rdAnswer.ID = this.ControlName;

//An event is added to traverse every row that is bound to the
//gridview to fetch the answer column.

rdAnswer.DataBinding += new EventHandler(this.OnDataBinding);
ListItem lst1 = new ListItem("Yes","0");
ListItem lst2 = new ListItem("No","1");
rdAnswer.Items.Add(lst1);
rdAnswer.Items.Add(lst2);
objContainer.Controls.Add(rdAnswer);

}

public void OnDataBinding(object sender, EventArgs e)
{
RadioButtonList rdAnswer = (RadioButtonList)sender;
GridViewRow container = (GridViewRow)rdAnswer.NamingContainer;

//Setting the value of the selected index - This is the replacement for the
eval.

rdAnswer.SelectedIndex =
int.Parse(((DataRowView)container.DataItem).Row.It emArray[2].ToString());

}

}

I guess you shd be all set with this code sample above.

- Parvathy Padmanabhan
Mar 30 '07 #5
Parvathy,

Excellent. I will try this and let you know.
Thanks,

On Mar 30, 6:46 pm, Parvathy Padmanabhan
<ParvathyPadmanab...@discussions.microsoft.comwrot e:
Hi,

To generate a gridView programmatically, you need to create a GridView
object and assign the properties that you had mentioned.
BoundField objects can be created and added to the gridview object.
Problem comes only when you need to add an ItemTemplate column. I tried a
sample code for the exact same gridView code that you had posted.

//My sample datatable which i am using to bind to the grid view.
DataTable dtSource = new DataTable();
dtSource.Columns.Add("QuestionID");
dtSource.Columns.Add("QuestionDesc");
dtSource.Columns.Add("Answer");
dtSource.Columns.Add("isHeading");

DataRow dr = dtSource.NewRow();
dr[0] = "Q1";
dr[1] = "I like reading books ";
dr[2] = "1";
dr[3] = "true";
dtSource.Rows.Add(dr);

dr = dtSource.NewRow();
dr[0] = "Q2";
dr[1] = "I watch a lot of TV";
dr[2] = "0";
dr[3] = "false";
dtSource.Rows.Add(dr);

//Creating a gridView

GridView gvBasic = new GridView();

gvBasic.DataKeyNames="QuestionID,isHeading".Split( ",".ToCharArray());
gvBasic.AutoGenerateColumns = false;
gvBasic.AllowPaging = true;
gvBasic.PageSize = 100;
gvBasic.DataSource = dtSource;

//Your column 1 that is hidden

BoundField objCol1 = new BoundField();
objCol1.Visible= false;
objCol1.DataField = "QuestionID";
objCol1.HeaderText = "QuestionID";
gvBasic.Columns.Add(objCol1);

//Your column 2 that has the questions

BoundField objCol2 = new BoundField();
objCol2.DataField = "QuestionDesc";
objCol2.HeaderText = "Question";
objCol2.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
gvBasic.Columns.Add(objCol2);

//ItemTemplate is an Interface in dotnet and hence,
//Create an ItemTemplate class which implements the interface.

// Your column three which has the radiobuttonList on it

TemplateField objCol3 = new TemplateField();
objCol3.HeaderText = "Answer";
ItemTemplate objTemplate = new ItemTemplate("rdAnswer", dtSource);
objCol3.ItemTemplate = objTemplate;

gvBasic.Columns.Add(objCol3);

gvBasic.DataBind();
Page.Form.Controls.Add(gvBasic);

Code for the ItemTemplate class:

public class ItemTemplate : ITemplate
{

string ControlName = "";
object DataSource = null;

//ControlName: Name of the radio button list.
// DataSource is the datatable to which the gridview is bound to.

public ItemTemplate(string ControlName,object DataSource)
{
this.ControlName = ControlName;
this.DataSource = DataSource;
}

public void InstantiateIn(Control objContainer)
{
RadioButtonList rdAnswer = new RadioButtonList();
rdAnswer.ID = this.ControlName;

//An event is added to traverse every row that is bound to the
//gridview to fetch the answer column.

rdAnswer.DataBinding += new EventHandler(this.OnDataBinding);
ListItem lst1 = new ListItem("Yes","0");
ListItem lst2 = new ListItem("No","1");
rdAnswer.Items.Add(lst1);
rdAnswer.Items.Add(lst2);
objContainer.Controls.Add(rdAnswer);

}

public void OnDataBinding(object sender, EventArgs e)
{
RadioButtonList rdAnswer = (RadioButtonList)sender;
GridViewRow container = (GridViewRow)rdAnswer.NamingContainer;

//Setting the value of the selected index - This is the replacement for the
eval.

rdAnswer.SelectedIndex =
int.Parse(((DataRowView)container.DataItem).Row.It emArray[2].ToString());

}

}

I guess you shd be all set with this code sample above.

- Parvathy Padmanabhan

Apr 3 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: | last post by:
I'm wondering if it's possible, outside of the declaration of an ASP.NET 2.0 GridView, to programmatically style individual columns. For example, I might want to make the text in a particular...
5
by: Brian McClellan | last post by:
Just wondering if anyone has a simple example of creating a gridview completely programmatically, i'm not doing anything terribly sophisticated. When creating the gridview declaratively evertying...
0
by: Chris | last post by:
Hi, Can I add a new row to gridview programmatically, similar to ADD method of listbox ? Thanks a lot in advance.
1
by: pApAk | last post by:
Hi, i am working at .NET Web-Application (c# codebehind) and i have placed on my web-site a GridView, aka DataGrid (that works as Server control). This GridView generated following code in...
2
by: Philip | last post by:
I can use a GridView and ObjectDataSource to bind my data using "Eval" expressions ... provided the GridView and related Column TemplateFields are defined as inline HTML. I prefer to...
0
by: cook.jonathan.m | last post by:
I want to programmatically add a dropdown to each row in a certain column returned to the gridview by my query. I want to do this entirely in behind-code. My gridview tag in the page will have...
4
by: Tomasz | last post by:
Hello Developers, Here is interesting problem I just came across: how do I wire a GridView control programmatically? Here is my sample code using Object Data Source: protected void...
0
by: Don Miller | last post by:
Without a SQLDataSource declaratively linked to a GridView, I programmatically create a dataset and bind it to a GridView with EnableSortingAndPagingCallbacks=True and Paging=True. The GridView...
3
by: =?Utf-8?B?QW5nZWw=?= | last post by:
I seem to understand how the control works as long as I mated to SqlDataSource or ObjDataSource it works fine. But I do not want to flatten my design in this manner. I am interested in how I can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.