How to add controls to panel in asp.net 2.0

V

vmssanthosh

hi
Actually, i am trying to add controls dynamically to Gridview cells in
asp.net 2.0.. but the controls are not getting added in the gridview..
i tried to add those controls in panel.. it also not working... i
tried it in visual studio 2003.. my code is working fine with 2003.. is
there is any syntax change in 2005... pls help me...
This is the code that what i am trying


DropDownList ddl4gdvw=new DropDownList();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddl4gdvw.Items.Add("Girisha");
ddl4gdvw.Items.Add("VMSSanthosh");
ddl4gdvw.Items.Add("SRaja");
ddl4gdvw.Visible = true;
Panel1.Controls.Add(ddl4gdvw);
Panel1.BackColor = System.Drawing.Color.Aqua;

DataTable dtgdvwddl = new DataTable();
dtgdvwddl.Columns.Add("something").DefaultValue="Hi";
dtgdvwddl.Columns.Add("Nothing").DefaultValue="Bye";
for(int i=0;i<5;i++)
dtgdvwddl.Rows.Add(dtgdvwddl.NewRow());
gdvwWithddl.DataSource = dtgdvwddl;
gdvwWithddl.DataBind();
}
}
protected void gdvwWithddl_RowDataBound(object sender,
GridViewRowEventArgs e)
{
e.Row.Cells[1].Controls.Add(ddl4gdvw);
}
}
 
C

CaffieneRush

GridView was introduced in ASP.NET 2.0 (VS 2005) so it is not possible
for your code to be working fine in VS 2003.
So I'm guessing, you may have meant the DataGrid webcontrol.
 
V

vmssanthosh

GridView was introduced in ASP.NET 2.0 (VS 2005) so it is not possible
for your code to be working fine in VS 2003.
So I'm guessing, you may have meant the DataGrid webcontrol.


yea i tried it with datagrid control the above is functioning..but not
with gridview
for panel too its not function i given the panel also in the above
code.. help me plz
 
C

CaffieneRush

The code which is adapted from an example from MSDN below assumes you
have SQLServer and the NorthWind database installed.

I used BoundFields to represent columns in the GridView but you can
also use ButtonField, CheckBoxField, CommandField, HyperLinkField,
ImageField and TemplateField within the GridView.

Regards,
Andy

<%@ Page Language="VB" %>
<%@ import namespace="System.Data" %>
<%@ import namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data.Common" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
If Not IsPostBack Then
' Declare the query string.
Dim queryString As String = _
"Select [CustomerID], [CompanyName], [Address], [City],
[PostalCode], [Country] From [Customers]"

' Run the query and bind the resulting DataSet
' to the GridView control.
Dim ds As DataSet = GetData(queryString)

' Manually setup columns
SetColumns(GridView1)

If (ds.Tables.Count > 0) Then

GridView1.DataSource = ds
GridView1.DataBind()

Else

Message.Text = "Unable to connect to the database."

End If
End If
End Sub

Private Sub SetColumns(ByRef Grid As GridView)
Dim column As BoundField
'Create/Set/Add the CustomerID bound column
column = New BoundField
column.DataField = "CustomerID"
column.ReadOnly = True
column.HeaderText = "Customer ID"
Grid.Columns.Add(column)

'CompanyName
column = New BoundField
column.DataField = "CompanyName"
column.ConvertEmptyStringToNull = True
column.HeaderText = "Company Name"
Grid.Columns.Add(column)

'Address
column = New BoundField
column.DataField = "Address"
column.ConvertEmptyStringToNull = True
column.HeaderText = "Address"
Grid.Columns.Add(column)
End Sub

Function GetData(ByVal queryString As String) As DataSet

' Retrieve the connection string stored in the Web.config file.
'<add name="NorthwindConnectionString" connectionString="Data
Source=localhost;Initial Catalog=Northwind;Integrated Security=True"
'providerName="System.Data.SqlClient" />
Dim connectionString As String =
ConfigurationManager.ConnectionStrings("NorthWindConnectionString").ConnectionString
Dim providerName As String =
ConfigurationManager.ConnectionStrings("NorthWindConnectionString").ProviderName
Dim ds As New DataSet()

Try

' Connect to the database and run the query.
Dim provider As DbProviderFactory =
DbProviderFactories.GetFactory(providerName)
Dim connection As DbConnection = provider.CreateConnection
connection.ConnectionString = connectionString

Dim selectCommand As DbCommand = connection.CreateCommand
selectCommand.CommandText = queryString

Dim adapter As DbDataAdapter = provider.CreateDataAdapter
adapter.SelectCommand = selectCommand

' Fill the DataSet.
adapter.Fill(ds)

Catch ex As Exception

' The connection failed. Display an error message.
Message.Text = "Unable to connect to the database."

End Try

Return ds

End Function

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Demo Programmatical Addition of Column to Grid</title>
</head>
<body>
<form id="form1" runat="server" onload="form1_Load">
<div>
<asp:label id="Message"
forecolor="Red"
runat="server"/>

<br/>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="false">
</asp:GridView>
</div>
</form>
</body>
</html>
 
V

vmssanthosh

Thank u andy

but my problem is ... i hav to add controls dynamically to the
gridview... on executing the code what i sent its not showing any error
but controls are not adding
The code which is adapted from an example from MSDN below assumes you
have SQLServer and the NorthWind database installed.

I used BoundFields to represent columns in the GridView but you can
also use ButtonField, CheckBoxField, CommandField, HyperLinkField,
ImageField and TemplateField within the GridView.

Regards,
Andy

<%@ Page Language="VB" %>
<%@ import namespace="System.Data" %>
<%@ import namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data.Common" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
If Not IsPostBack Then
' Declare the query string.
Dim queryString As String = _
"Select [CustomerID], [CompanyName], [Address], [City],
[PostalCode], [Country] From [Customers]"

' Run the query and bind the resulting DataSet
' to the GridView control.
Dim ds As DataSet = GetData(queryString)

' Manually setup columns
SetColumns(GridView1)

If (ds.Tables.Count > 0) Then

GridView1.DataSource = ds
GridView1.DataBind()

Else

Message.Text = "Unable to connect to the database."

End If
End If
End Sub

Private Sub SetColumns(ByRef Grid As GridView)
Dim column As BoundField
'Create/Set/Add the CustomerID bound column
column = New BoundField
column.DataField = "CustomerID"
column.ReadOnly = True
column.HeaderText = "Customer ID"
Grid.Columns.Add(column)

'CompanyName
column = New BoundField
column.DataField = "CompanyName"
column.ConvertEmptyStringToNull = True
column.HeaderText = "Company Name"
Grid.Columns.Add(column)

'Address
column = New BoundField
column.DataField = "Address"
column.ConvertEmptyStringToNull = True
column.HeaderText = "Address"
Grid.Columns.Add(column)
End Sub

Function GetData(ByVal queryString As String) As DataSet

' Retrieve the connection string stored in the Web.config file.
'<add name="NorthwindConnectionString" connectionString="Data
Source=localhost;Initial Catalog=Northwind;Integrated Security=True"
'providerName="System.Data.SqlClient" />
Dim connectionString As String =
ConfigurationManager.ConnectionStrings("NorthWindConnectionString").ConnectionString
Dim providerName As String =
ConfigurationManager.ConnectionStrings("NorthWindConnectionString").ProviderName
Dim ds As New DataSet()

Try

' Connect to the database and run the query.
Dim provider As DbProviderFactory =
DbProviderFactories.GetFactory(providerName)
Dim connection As DbConnection = provider.CreateConnection
connection.ConnectionString = connectionString

Dim selectCommand As DbCommand = connection.CreateCommand
selectCommand.CommandText = queryString

Dim adapter As DbDataAdapter = provider.CreateDataAdapter
adapter.SelectCommand = selectCommand

' Fill the DataSet.
adapter.Fill(ds)

Catch ex As Exception

' The connection failed. Display an error message.
Message.Text = "Unable to connect to the database."

End Try

Return ds

End Function

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Demo Programmatical Addition of Column to Grid</title>
</head>
<body>
<form id="form1" runat="server" onload="form1_Load">
<div>
<asp:label id="Message"
forecolor="Red"
runat="server"/>

<br/>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="false">
</asp:GridView>
</div>
</form>
</body>
</html>
yea i tried it with datagrid control the above is functioning..but not
with gridview
for panel too its not function i given the panel also in the above
code.. help me plz
 
C

CaffieneRush

Yes you have added controls to your gridview but you cannot just add
any control to the GridView and expect it to work.
And as I mentioned before the fields you can add to the GridView are
BoundFields, ButtonField, CheckBoxField, CommandField, HyperLinkField,
ImageField and TemplateField. These are the controls that can properly
bind to your datasource when DataBind() is called.

Look at the SetColumns() method again. You need to programmatically or
declaratively add columns (called fields in GridView) to your GridView
and configure the columns to a particular DataField in your datasource.
Most of you work lies in setting up the GridView before calling
DataBind().

Regards,
Andy
Thank u andy

but my problem is ... i hav to add controls dynamically to the
gridview... on executing the code what i sent its not showing any error
but controls are not adding
The code which is adapted from an example from MSDN below assumes you
have SQLServer and the NorthWind database installed.

I used BoundFields to represent columns in the GridView but you can
also use ButtonField, CheckBoxField, CommandField, HyperLinkField,
ImageField and TemplateField within the GridView.

Regards,
Andy

<%@ Page Language="VB" %>
<%@ import namespace="System.Data" %>
<%@ import namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data.Common" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
If Not IsPostBack Then
' Declare the query string.
Dim queryString As String = _
"Select [CustomerID], [CompanyName], [Address], [City],
[PostalCode], [Country] From [Customers]"

' Run the query and bind the resulting DataSet
' to the GridView control.
Dim ds As DataSet = GetData(queryString)

' Manually setup columns
SetColumns(GridView1)

If (ds.Tables.Count > 0) Then

GridView1.DataSource = ds
GridView1.DataBind()

Else

Message.Text = "Unable to connect to the database."

End If
End If
End Sub

Private Sub SetColumns(ByRef Grid As GridView)
Dim column As BoundField
'Create/Set/Add the CustomerID bound column
column = New BoundField
column.DataField = "CustomerID"
column.ReadOnly = True
column.HeaderText = "Customer ID"
Grid.Columns.Add(column)

'CompanyName
column = New BoundField
column.DataField = "CompanyName"
column.ConvertEmptyStringToNull = True
column.HeaderText = "Company Name"
Grid.Columns.Add(column)

'Address
column = New BoundField
column.DataField = "Address"
column.ConvertEmptyStringToNull = True
column.HeaderText = "Address"
Grid.Columns.Add(column)
End Sub

Function GetData(ByVal queryString As String) As DataSet

' Retrieve the connection string stored in the Web.config file.
'<add name="NorthwindConnectionString" connectionString="Data
Source=localhost;Initial Catalog=Northwind;Integrated Security=True"
'providerName="System.Data.SqlClient" />
Dim connectionString As String =
ConfigurationManager.ConnectionStrings("NorthWindConnectionString").ConnectionString
Dim providerName As String =
ConfigurationManager.ConnectionStrings("NorthWindConnectionString").ProviderName
Dim ds As New DataSet()

Try

' Connect to the database and run the query.
Dim provider As DbProviderFactory =
DbProviderFactories.GetFactory(providerName)
Dim connection As DbConnection = provider.CreateConnection
connection.ConnectionString = connectionString

Dim selectCommand As DbCommand = connection.CreateCommand
selectCommand.CommandText = queryString

Dim adapter As DbDataAdapter = provider.CreateDataAdapter
adapter.SelectCommand = selectCommand

' Fill the DataSet.
adapter.Fill(ds)

Catch ex As Exception

' The connection failed. Display an error message.
Message.Text = "Unable to connect to the database."

End Try

Return ds

End Function

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Demo Programmatical Addition of Column to Grid</title>
</head>
<body>
<form id="form1" runat="server" onload="form1_Load">
<div>
<asp:label id="Message"
forecolor="Red"
runat="server"/>

<br/>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="false">
</asp:GridView>
</div>
</form>
</body>
</html>
(e-mail address removed) wrote:
GridView was introduced in ASP.NET 2.0 (VS 2005) so it is not possible
for your code to be working fine in VS 2003.
So I'm guessing, you may have meant the DataGrid webcontrol.


yea i tried it with datagrid control the above is functioning..but not
with gridview
for panel too its not function i given the panel also in the above
code.. help me plz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top