Beanie posted on February 17, 2009 13:10

While I had five minutes to spare I thought I put up a gridview I've just written. This one display, allows editing and inserting of a Date and Score against a master record (fldMasterID). the two editable fields are both DropDownLists the date field populates from a database table and the score is generated in code (FillScore, see below). The example below shows how I've done this, there are a few bits removed for clarity, the data source for the dropdowns, style classes etc.

 

The GridView is the main data handler for this example we take the data that is stored the Date (from the date table) and the Score (from the MasterDate Table) (joined on DateID) based on the MasterID, we also need the DateID and The MasterDateID for use in the update command.

The ItemTemplate shows our data as it is Date and Score are display as they are stored, the EditTemplate shows the drop downs with the current values selected and finally theFooterTemplate show theDropDowns for the final row which allows the insert.

<asp:GridView ID="gvDate" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" 
DataKeyNames="fldMasterDateID" DataSourceID="#DataSource#"     ShowFooter="True" >
<Columns> 
<asp:TemplateField>
<ItemTemplate>
<asp:Button Text="Edit" CommandName="Edit" CausesValidation="false" runat="server" ID="btEdit" />&nbsp;
<asp:Button Text="Delete" CommandName="Delete" CausesValidation="false" runat="server" ID="btDelete" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button Text="Update" CommandName="Update" CausesValidation="false" runat="server" ID="btUpdate" />&nbsp;
<asp:Button Text="Cancel" CommandName="Cancel" CausesValidation="false" runat="server" ID="btCancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button Text="Insert" CommandName="Insert" CausesValidation="false" runat="server" ID="btInsert" OnClick="lbInsert_Click" />&nbsp;
<asp:Button Text="Cancel" CommandName="Cancel" CausesValidation="false" runat="server" ID="btCancel" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date" SortExpression="fldDateID" Visible="True">
<ItemTemplate>
<asp:Label ID="lblDate" runat="server" Text='<%# Eval("fldDate") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="cboDateID" runat="server" DataSourceID="#DataSource#" DataValueField="fldDateID" 
DataTextField="fldDate" selectedValue='<%# Bind("fldDateID") %>'>
</asp:DropDownList> 
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="cboDateID" runat="server" DataSourceID="#DataSource#" DataValueField="fldDateID"
DataTextField="fldDate" SelectedValue='<%# Bind("fldDateID") %>'>
</asp:DropDownList> 
</FooterTemplate> 
</asp:TemplateField>
<asp:TemplateField HeaderText="Score" SortExpression="fldScore">
<ItemTemplate>
<asp:Label ID="lblScore" runat="server" Text='<%# Bind("fldScore") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="cboScore" runat="server" DataSource="<%# FillScoreDS() %>" SelectedValue='<%# Bind("fldScore") %>'> 
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="cboScore" runat="server" DataSource="<%# FillScoreDS() %>"> 
</asp:DropDownList> 
</FooterTemplate> 
</asp:TemplateField>     
</Columns>
</asp:GridView>

This is theDataSource fr the GridView showing the Select, Update and Insert specifications

<asp:SqlDataSource ID="dsDate" runat="server" 
ConnectionString="<%$ #ConnectionString# %>"     SelectCommand="select"     SelectCommandType="StoredProcedure" 
InsertCommand="insert" InsertCommandType="StoredProcedure" UpdateCommand="update" UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="0" Name="MasterID" QueryStringField="MasterID" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="fldDateID" Type="Int32" />
<asp:Parameter Name="fldScore" Type="Int32" />
<asp:QueryStringParameter Name="fldID" Type="Int32" QueryStringField="ID" /> 
<asp:Parameter Name="fldMasterDateID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="fldDateID" Type="Int32" />
<asp:Parameter Name="fldScore" Type="Int32" /> 
<asp:QueryStringParameter Name="fldMasterID" Type="Int32" QueryStringField="MasterID" />
</InsertParameters>
</asp:SqlDataSource>

This is the code that runs the Insert procedure, firstly we set the InsertParameters to the values selected in the DropDownLists, then we run the Insert command followed by a DataBind on the GridView.

protected void lbInsert_Click(object sender, EventArgs e)
{
DropDownList cboScore = (DropDownList)gvDate.FooterRow.FindControl("cboScore");
DropDownList cboDateID = (DropDownList)gvDate.FooterRow.FindControl("cboDateID");
if (cboScore != null && cboDateID != null)
{
DataSource.InsertParameters["fldDateID"].DefaultValue = cboDateID.SelectedValue.ToString();
DataSource.InsertParameters["fldScore"].DefaultValue = cboScore.SelectedValue.ToString();
DataSource.Insert();
}
gvDate.DataBind();
}  

This function populates our ScoreDropDownList with the numbers 0-38.

protected ListItemCollection FillScoreDS()
{
ListItemCollection listBoxData = new ListItemCollection();
int i = 0;
for(i=0;i<=38;i++)
{
ListItem li = new ListItem(i.ToString(), i.ToString());
listBoxData.Add(li);
}
return listBoxData;
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Add comment


 
  Country flag

biuquote
  • Comment
  • Preview
Loading



Page List

Search Blog

Tag Cloud

Recent Comments

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010 Beanie