Write an Article Write a Tutorial Add a Fast Code* *Fast Code might be any helpful code with brief comment
Stem Cells Blood Banks Insurances .Net Hosting Credit Cards PC Rental Business
Tutorials: ASP .NET C# .NET VB .NET Articles: ASP .NET C# .NET VB .NET Fast Code: ASP .NET C# .NET VB .NET
Let us see how does our Gridview in .aspx Page look like.
Our .aspx Page;
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns=false OnRowUpdating="GridView1_RowUpdating" > <Columns> <asp:CommandField ShowEditButton="True" ShowSelectButton=true /> <asp:BoundField DataField="id" HeaderText="id" SortExpression="id" /> <asp:BoundField DataField="message" HeaderText="message" SortExpression="message" /> <asp:TemplateField HeaderText="User Name"> <ItemTemplate> <asp:Label ID="lbl1" runat="server" Text='<%# Bind("username")%>' /> </ItemTemplate> <EditItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true" DataSourceID="SqlDataSource1" DataValueField=username AutoPostBack="True" SelectedValue='<%# Bind("username")%>'> <asp:ListItem Value="">Select an Item</asp:ListItem> </asp:DropDownList> </EditItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
Our Dropdownlist is embeded in the "ItemTemplete" of the Gridview. One more thing before going next, Always use Bind() with the Controles in both ItemTemplate and EditItemTemplate. For this Demo we are using SqlDataSource Object as DataSource. Now let us see how does "SqlDataSource" looke like.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PersonalLocal %>" SelectCommand="SELECT * FROM [ChatMessages]" UpdateCommand="UPDATE [ChatMessages] SET [username] = @username, [message] = @message WHERE id=@id"> <DeleteParameters> <asp:Parameter Name="id" Type="Int64" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="message" Type="String" /> <asp:Parameter Name="online" Type="Boolean" /> <asp:Parameter Name="id" Type="Int64" /> <asp:Parameter Name="username" Type="String" /> </UpdateParameters> </asp:SqlDataSource>
You noticed that in the Gridview Code we defined an event called "RowUpdating" In the code behind we will show the event code. Now, let us see how does the code behind look like.
Code behind for the Row Update event;
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { string struName = ((DropDownList)((GridView)sender).Rows[e.RowIndex].FindControl("DropDownList1")).SelectedValue; e.NewValues["username"] = struName; e.Cancel = false; }
I understand the code yes but simply do not understand your intetion. Did you honestly explain any thing to the newbies
30/12/2006 03:27:47 UTC
Hi kip,
Please, help us improving.
30/12/2006 16:39:52 UTC
02/02/2007 23:35:05 UTC
Omerkamal,
I had a GridView and there is a dropdownlist in the GridView. Here is my problem when i was clicking on the EDIT button it is displaying an error.
Parameter name: value 'ddlConfiguration' has a SelectedValue which is invalid because it does not exist in the list of items. Can u plz help me in this?
11/07/2007 15:38:08 UTC
That means that the value which is suppoed to be selcted does not exsit in the ConfigNameDS Database or you are accessing different Fields of the Database.
The Values must lie in both the databases. The Database you attaching to the Gridview and the one you attaching to the DropDownList. Moreover, your Configuration Field must represent the same field which you bouding to the DropDownList.
12/07/2007 03:09:58 UTC
am doing one project in that create 2 textboxs enter one textbox a number then show the 2nd textbox result multiplication of that number with 7
20/09/2007 00:40:44 UTC
Hi,
18/10/2007 03:16:05 UTC