Binding a DropDownList to a cell in a grid...

R

rmorvay

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;
}
 
K

Karunakararao

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;database=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.CloseConnection)
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;database=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.CloseConnection)
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;database=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.CloseConnection)
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.FindByText(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;database=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.CloseConnection)
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>
 
R

rmorvay

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..
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top