I am trying to dynamically build a dropdownlist and bind it to a cell in a
grid. I tried to utilize the following code but I am stuck at the point
where I bind the dropdownlist to the grid cell. I get the following message
for this code but I suspect that the "
UltraWebGrid1.Rows[intCtr].Cells[3].Value = ddlGeography;" is not how you
bind the dropdownlist control to the grid. ERROR: "The type
'System.Web.UI.WebControls.DropDownList' must be marked as Serializable or
have a TypeConverter other than ReferenceConverter to be put in viewstate."
Any insight would be appreciated. This is test code so don't worry about
hte fact that each grid cell will have one dropdownlist control with IL and
60014 as the only row.
for (int intCtr = 0; intCtr <= UltraWebGrid1.Rows.Count - 1;intCtr++)
{
DataSet dsGeography = new DataSet();
DataTable dtGeography = dsGeography.Tables.Add("Geography");
dtGeography.Columns.Add("State", typeof(string));
dtGeography.Columns.Add("ZipCode", typeof(string));
DataRow dr;
dr = dsGeography.Tables[0].NewRow();
dr["State"] = "IL";
dr["ZipCode"] = "60014";
DropDownList ddlGeography = new DropDownList();
ddlGeography.DataSource = dsGeography;
ddlGeography.DataBind();
UltraWebGrid1.Rows[intCtr].Cells[3].Value = ddlGeography;
dsGeography = null;
dtGeography = null;
dr = null;
}
--
rmorvay
--
Snubs, Illinois
VROC : 12407
WindyCity VROC
2003 Black 1500 Vulcan Nomad FI (Lots of extras...)
2002 Black 750 Honda Magna 2 2203
Hi
Use this code it's working fine
Regards
Venu.
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<HTML>
<HEAD>
<title>DataGrid with Combo Box - by Das.</title>
<script language="vb" runat="server">
Dim strCurrentState as String
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Page.IsPostBack Then
BindGrid()
End If
End Sub
Private Sub BindGrid()
' Create Instance of Connection and Command Object
Dim myConnection As SqlConnection = New
SqlConnection("server=192.168.0.44;uid=sa;pwd=sa;d atabase=GLIN")
Dim myCommand As SqlCommand = New
SqlCommand("sp_stores_sel",myConnection)
Dim myDataReader As SqlDataReader
' Mark the Command as a SPROC
myCommand.CommandType = CommandType.StoredProcedure
Try
myConnection.Open()
myDataReader =
myCommand.ExecuteReader(CommandBehavior.CloseConne ction)
DG_Combo.DataSource = myDataReader
DG_Combo.DataBind()
Catch SQLexc As SqlException
lblStatus.Text = "Error while Generating Data. Error
is " & SQLexc.ToString()
Finally
If Not myDataReader Is Nothing Then
myDataReader.Close()
End If
If Not myConnection Is Nothing AndAlso
((myConnection.State And ConnectionState.Open) = ConnectionState.Open) Then
myConnection.Close()
End If
End Try
End Sub
Public Sub DG_Combo_Cancel(sender As Object, e As DataGridCommandEventArgs)
lblStatus.text = "Edit Cancelled."
DG_Combo.EditItemIndex = -1
BindGrid()
End Sub
Sub DG_Combo_Edit(Sender As Object, E As DataGridCommandEventArgs)
lblStatus.text = "Edit Mode."
DG_Combo.EditItemIndex = CInt(E.Item.ItemIndex)
strCurrentState =
Ucase(CType(e.Item.FindControl("lblState"), Label).Text)
BindGrid()
End Sub
Public Sub DG_Combo_Update(sender As Object, e As DataGridCommandEventArgs)
Dim myConnection As SqlConnection = New
SqlConnection("server=192.168.0.44;uid=sa;pwd=sa;d atabase=GLIN")
Dim mySqlCommand as SqlCommand
Dim strStoreID as String =
CType(e.Item.FindControl("hdnStoreID"), TextBox).Text
Dim cboStateTemp As DropDownList =
CType(e.Item.FindControl("cboState"), DropDownList)
Dim strSql as String
strSql = "exec sp_update_pubs_stores '" & strStoreID & "',"
strSql = strSql & cboStateTemp.SelectedItem.Value
mySqlCommand = new SqlCommand(strSql, myConnection)
Try
lblStatus.text = "Update failed:"
myConnection.Open()
mySqlCommand.ExecuteReader(CommandBehavior.CloseCo nnection)
lblStatus.text = "UPDATED Successfully!"
Catch exc as exception
if Instr(1, exc.tostring, "duplicate key") > 0 then
lblStatus.text = lblStatus.text + "Cannot
insert duplicate values."
else
lblStatus.text = lblStatus.text +
exc.tostring()
end if
Catch SQLexc as sqlexception
lblStatus.text = lblStatus.text + sqlexc.tostring()
Finally
myConnection.Close()
End Try
DG_Combo.EditItemIndex = -1
BindGrid()
End Sub
Private Sub DG_ItemDataBound(s as object, e as DataGridItemEventArgs)
If e.item.itemType = ListItemType.EditItem Then
Dim myDropDown as DropDownList
Dim myConnection As SqlConnection = New
SqlConnection("server=192.168.0.44;uid=sa;pwd=sa;d atabase=GLIN")
Dim myCommand As SqlCommand = New SqlCommand("sp_das_state_sel",
myConnection)
Dim myDataReader As SqlDataReader
' Mark the Command as a SPROC
myCommand.CommandType = CommandType.StoredProcedure
Try
myConnection.Open()
myDataReader =
myCommand.ExecuteReader(CommandBehavior.CloseConne ction)
myDropDown =
Ctype(e.Item.FindControl("cbostate"), DropDownList)
myDropDown.DataTextField="Statename"
myDropDown.DataValueField="Statename"
myDropDown.DataSource=myDataReader
myDropDown.DataBind()
Catch SQLexc As SqlException
lblStatus.Text = "Error while Generating Data. Error
is " & SQLexc.ToString()
Finally
If Not myDataReader Is Nothing Then
myDataReader.Close()
End If
If Not myConnection Is Nothing AndAlso
((myConnection.State And ConnectionState.Open) = ConnectionState.Open) Then
myConnection.Close()
End If
End Try
myDropDown.SelectedIndex =
myDropDown.Items.IndexOf(myDropDown.Items.FindByTe xt(strCurrentState))
End If
End Sub
Public Sub PopulateNextCombo(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim tmpcboState2 As DropDownList
Dim tmpDropDown1 as DropDownList
Dim myConnection As SqlConnection = New
SqlConnection("server=192.168.0.44;uid=sa;pwd=sa;d atabase=GLIN")
Dim myCommand As SqlCommand = New SqlCommand("sp_das_state_sel",
myConnection)
Dim myDataReader As SqlDataReader
myCommand.CommandType = CommandType.StoredProcedure
Try
myConnection.Open()
myDataReader =
myCommand.ExecuteReader(CommandBehavior.CloseConne ction)
tmpcboState2 =
CType(DG_Combo.Items.Item(DG_Combo.EditItemIndex). FindControl("cboState2"),
DropDownList)
tmpcboState2.DataSource = myDataReader
tmpcboState2.DataBind()
tmpDropDown1 =
CType(DG_Combo.Items.Item(DG_Combo.EditItemIndex). FindControl("cboState"),
DropDownList)
tmpcboState2.SelectedIndex =
tmpDropDown1.SelectedIndex
Catch SQLexc As SqlException
lblStatus.Text = "Error while Generating Data. Error
is " & SQLexc.ToString()
Finally
If Not myDataReader Is Nothing Then
myDataReader.Close()
End If
If Not myConnection Is Nothing AndAlso
((myConnection.State And ConnectionState.Open) = ConnectionState.Open) Then
myConnection.Close()
End If
End Try
End Sub
</script>
</HEAD>
<body style="FONT: 10pt verdana">
<form id="Form1" method="post" runat="server">
<h2 align="center">DataGrid with Two Combo box</h2>
<asp:DataGrid ID="DG_Combo" AutoGenerateColumns="False" Width="99%"
HeaderStyle-BackColor="#ff0000" HeaderStyle-Font-Bold="True"
HeaderStyle-Font-Name="Verdana" HeaderStyle-Font-Size="13px"
HeaderStyle-ForeColor="#ffffff" ItemStyle-BackColor="Beige"
ItemStyle-Font-Name="verdana" ItemStyle-Font-Size="13px"
BorderColor="#000000" OnCancelCommand="DG_Combo_Cancel"
OnUpdateCommand="DG_Combo_Update" OnEditCommand="DG_Combo_Edit"
OnItemDataBound="DG_ItemDataBound" Runat="server">
<Columns>
<asp:editcommandcolumn edittext="Edit" canceltext="Cancel"
updatetext="Update" itemstyle-wrap="false" ItemStyle-Width="100px" />
<asp:TemplateColumn HeaderText="Store Name">
<ItemTemplate>
<asp:Label ID="lblStoreName" Text='<%#
DataBinder.Eval(Container.DataItem, "stor_name") %>' Runat=server />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Store Address">
<ItemTemplate>
<asp:Label ID="lblStoreAddress" Text='<%#
DataBinder.Eval(Container.DataItem, "stor_address") %>' Runat=server />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="City">
<ItemTemplate>
<asp:Label ID="lblCity" Text='<%# DataBinder.Eval(Container.DataItem,
"city") %>' Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="State">
<ItemTemplate>
<asp:Label ID="lblState" Text='<%#
DataBinder.Eval(Container.DataItem, "state") %>' Runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" id="hdnStoreID" Visible=False Text='<%#
DataBinder.Eval(Container.DataItem, "stor_id") %>'/>
<asp:Label ID="lblTempState" Text='<%#
DataBinder.Eval(Container.DataItem, "state") %>' Runat="server" />
<asp:DropDownList id="cboState"
OnSelectedIndexChanged="PopulateNextCombo" AutoPostBack="True"
runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Zip">
<ItemTemplate>
<asp:Label ID="lblZip" Text='<%# DataBinder.Eval(Container.DataItem,
"zip") %>' Runat=server />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="cboState2" DataTextField="Statename"
DataValueField="Statename" runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<asp:Label ID="lblStatus" Font-Name="verdana" Font-Size="12px"
ForeColor="#ff0000" Runat="server" />
</form>
</body>
</HTML>
I am not using the Microsoft Datagrid. I am using the Infragistic
UltraWebGrid. Plus I am developing in C# not vb.net. I will take a stab at
converting this to my use but I don't have the ItemDataBound event to work
with..
--
rmorvay
"Karunakararao" <ka************@i-vantage.com> wrote in message
news:OF**************@TK2MSFTNGP12.phx.gbl... Hi
Use this code it's working fine
Regards Venu.
<%@ Import Namespace="System.Data.SqlClient" %> <%@ Import Namespace="System.Data" %> <HTML> <HEAD> <title>DataGrid with Combo Box - by Das.</title> <script language="vb" runat="server">
Dim strCurrentState as String
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Page.IsPostBack Then BindGrid() End If
End Sub Private Sub BindGrid()
' Create Instance of Connection and Command Object
Dim myConnection As SqlConnection = New SqlConnection("server=192.168.0.44;uid=sa;pwd=sa;d atabase=GLIN") Dim myCommand As SqlCommand = New SqlCommand("sp_stores_sel",myConnection) Dim myDataReader As SqlDataReader ' Mark the Command as a SPROC myCommand.CommandType = CommandType.StoredProcedure Try myConnection.Open() myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConne ction) DG_Combo.DataSource = myDataReader DG_Combo.DataBind() Catch SQLexc As SqlException
lblStatus.Text = "Error while Generating Data.
Error is " & SQLexc.ToString() Finally
If Not myDataReader Is Nothing Then myDataReader.Close() End If
If Not myConnection Is Nothing AndAlso ((myConnection.State And ConnectionState.Open) = ConnectionState.Open)
Then myConnection.Close() End If
End Try
End Sub Public Sub DG_Combo_Cancel(sender As Object, e As
DataGridCommandEventArgs) lblStatus.text = "Edit Cancelled." DG_Combo.EditItemIndex = -1 BindGrid()
End Sub Sub DG_Combo_Edit(Sender As Object, E As DataGridCommandEventArgs)
lblStatus.text = "Edit Mode." DG_Combo.EditItemIndex = CInt(E.Item.ItemIndex) strCurrentState = Ucase(CType(e.Item.FindControl("lblState"), Label).Text) BindGrid()
End Sub Public Sub DG_Combo_Update(sender As Object, e As
DataGridCommandEventArgs) Dim myConnection As SqlConnection = New SqlConnection("server=192.168.0.44;uid=sa;pwd=sa;d atabase=GLIN") Dim mySqlCommand as SqlCommand Dim strStoreID as String = CType(e.Item.FindControl("hdnStoreID"), TextBox).Text Dim cboStateTemp As DropDownList = CType(e.Item.FindControl("cboState"), DropDownList) Dim strSql as String strSql = "exec sp_update_pubs_stores '" & strStoreID & "'," strSql = strSql & cboStateTemp.SelectedItem.Value mySqlCommand = new SqlCommand(strSql, myConnection) Try lblStatus.text = "Update failed:" myConnection.Open()
mySqlCommand.ExecuteReader(CommandBehavior.CloseCo nnection) lblStatus.text = "UPDATED Successfully!" Catch exc as exception if Instr(1, exc.tostring, "duplicate key") > 0 then lblStatus.text = lblStatus.text +
"Cannot insert duplicate values." else lblStatus.text = lblStatus.text + exc.tostring() end if Catch SQLexc as sqlexception lblStatus.text = lblStatus.text +
sqlexc.tostring() Finally myConnection.Close() End Try
DG_Combo.EditItemIndex = -1 BindGrid()
End Sub Private Sub DG_ItemDataBound(s as object, e as DataGridItemEventArgs)
If e.item.itemType = ListItemType.EditItem Then Dim myDropDown as DropDownList Dim myConnection As SqlConnection = New SqlConnection("server=192.168.0.44;uid=sa;pwd=sa;d atabase=GLIN") Dim myCommand As SqlCommand = New
SqlCommand("sp_das_state_sel", myConnection) Dim myDataReader As SqlDataReader
' Mark the Command as a SPROC myCommand.CommandType = CommandType.StoredProcedure Try myConnection.Open() myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConne ction) myDropDown = Ctype(e.Item.FindControl("cbostate"), DropDownList) myDropDown.DataTextField="Statename" myDropDown.DataValueField="Statename" myDropDown.DataSource=myDataReader myDropDown.DataBind()
Catch SQLexc As SqlException lblStatus.Text = "Error while Generating Data.
Error is " & SQLexc.ToString() Finally If Not myDataReader Is Nothing Then myDataReader.Close() End If If Not myConnection Is Nothing AndAlso ((myConnection.State And ConnectionState.Open) = ConnectionState.Open)
Then myConnection.Close() End If End Try myDropDown.SelectedIndex = myDropDown.Items.IndexOf(myDropDown.Items.FindByTe xt(strCurrentState)) End If
End Sub Public Sub PopulateNextCombo(ByVal sender As Object, ByVal e As System.EventArgs)
Dim tmpcboState2 As DropDownList
Dim tmpDropDown1 as DropDownList
Dim myConnection As SqlConnection = New SqlConnection("server=192.168.0.44;uid=sa;pwd=sa;d atabase=GLIN")
Dim myCommand As SqlCommand = New
SqlCommand("sp_das_state_sel", myConnection)
Dim myDataReader As SqlDataReader myCommand.CommandType = CommandType.StoredProcedure Try
myConnection.Open() myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConne ction) tmpcboState2 =
CType(DG_Combo.Items.Item(DG_Combo.EditItemIndex). FindControl("cboState2"), DropDownList) tmpcboState2.DataSource = myDataReader tmpcboState2.DataBind() tmpDropDown1 = CType(DG_Combo.Items.Item(DG_Combo.EditItemIndex). FindControl("cboState"), DropDownList) tmpcboState2.SelectedIndex = tmpDropDown1.SelectedIndex Catch SQLexc As SqlException lblStatus.Text = "Error while Generating Data.
Error is " & SQLexc.ToString() Finally If Not myDataReader Is Nothing Then myDataReader.Close() End If If Not myConnection Is Nothing AndAlso ((myConnection.State And ConnectionState.Open) = ConnectionState.Open)
Then myConnection.Close() End If End Try End Sub
</script> </HEAD> <body style="FONT: 10pt verdana"> <form id="Form1" method="post" runat="server"> <h2 align="center">DataGrid with Two Combo box</h2> <asp:DataGrid ID="DG_Combo" AutoGenerateColumns="False" Width="99%" HeaderStyle-BackColor="#ff0000" HeaderStyle-Font-Bold="True" HeaderStyle-Font-Name="Verdana" HeaderStyle-Font-Size="13px" HeaderStyle-ForeColor="#ffffff" ItemStyle-BackColor="Beige" ItemStyle-Font-Name="verdana" ItemStyle-Font-Size="13px" BorderColor="#000000" OnCancelCommand="DG_Combo_Cancel" OnUpdateCommand="DG_Combo_Update" OnEditCommand="DG_Combo_Edit" OnItemDataBound="DG_ItemDataBound" Runat="server"> <Columns> <asp:editcommandcolumn edittext="Edit" canceltext="Cancel" updatetext="Update" itemstyle-wrap="false" ItemStyle-Width="100px" /> <asp:TemplateColumn HeaderText="Store Name"> <ItemTemplate> <asp:Label ID="lblStoreName" Text='<%# DataBinder.Eval(Container.DataItem, "stor_name") %>' Runat=server /> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="Store Address"> <ItemTemplate> <asp:Label ID="lblStoreAddress" Text='<%# DataBinder.Eval(Container.DataItem, "stor_address") %>' Runat=server /> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="City"> <ItemTemplate> <asp:Label ID="lblCity" Text='<%#
DataBinder.Eval(Container.DataItem, "city") %>' Runat="server" /> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="State"> <ItemTemplate> <asp:Label ID="lblState" Text='<%# DataBinder.Eval(Container.DataItem, "state") %>' Runat="server" /> </ItemTemplate> <EditItemTemplate> <asp:TextBox runat="server" id="hdnStoreID" Visible=False Text='<%# DataBinder.Eval(Container.DataItem, "stor_id") %>'/> <asp:Label ID="lblTempState" Text='<%# DataBinder.Eval(Container.DataItem, "state") %>' Runat="server" /> <asp:DropDownList id="cboState" OnSelectedIndexChanged="PopulateNextCombo" AutoPostBack="True" runat="server" /> </EditItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="Zip"> <ItemTemplate> <asp:Label ID="lblZip" Text='<%#
DataBinder.Eval(Container.DataItem, "zip") %>' Runat=server /> </ItemTemplate> <EditItemTemplate> <asp:DropDownList id="cboState2" DataTextField="Statename" DataValueField="Statename" runat="server" /> </EditItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid> <asp:Label ID="lblStatus" Font-Name="verdana" Font-Size="12px" ForeColor="#ff0000" Runat="server" /> </form> </body> </HTML>
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: DotNetJunkies User |
last post by:
Hie,
I create a dynamique HtmlTable, in each cell of this HtmlTable put a new control ( dropdownlist,label,..) and then want to create handler so that if i change the select item in the dop downlist...
|
by: ShadowsOfTheBeast |
last post by:
hi all i am trying to bind data to my datagrid from a listbox which i think
it should work but an error is coming up saying i have to bind to a
datasource that implements the Inumerable or...
|
by: Harold |
last post by:
Hi all, having a little difficulty binding up a dropdownlist control in a datagrid column. I know I must be missing something
Here are 2 methods. One is the DataGrid's ItemDataBound event to catch...
|
by: rmorvay |
last post by:
I am trying to dynamically build a dropdownlist and bind it to a cell in a
grid. I tried to utilize the following code but I am stuck at the point
where I bind the dropdownlist to the grid cell. ...
|
by: Frank |
last post by:
Hello,
I am developing with VS.Net 2003 on the asp 1.1 platform.
I am a few days new to using a datagrid. I found a nice tutorial and had no
problems using an editable datagrid with textboxes...
|
by: p.mc |
last post by:
Hi all,
I'm having major problems with a userControl which contains a datagrid.
My problem concerns data binding.
The Page_Load() procedure calls the DataBind procedure to bind the
datagrid...
|
by: AdrEsqu |
last post by:
I am trying to use AutoPostback to retrieve the value that was selected
in my dropdownlist. The event is not firing for the dropdownlist
because the datagrid is being built after the Lifecycle...
|
by: glenn |
last post by:
Hi folks,
I have a DropDownList in a DataGrid that is populated from records in a
database.
I want to add a value that might be a string such as "Select a Company" for
the first item since an...
|
by: jchaturv |
last post by:
All,
I have a customized DataGridView where I have implemented extra functionality and bounded datasource in the same custom DataGridView (Using
C# and .NET). Now, I could able to use it...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |