i have a datagrid with boundcolumns. unfortunately, due to business rules, I have to use bound columns for displaying my editable data. I otherwise would use a template column and put and editItemtemplate tag in. the edit-mode textboxes basically need to be decimals(scale 2). if the user doesn't put anything in the textbox (leaves it null or blank) i want to send a zero to the db. here's my code:
1 <asp:datagrid id="dgUpdateAnnual" Runat="server" AutoGenerateColumns="False" Font-Size="X-Small"
2 Width="100%" Font-Names="Tahoma" BorderWidth="1px" DataKeyField="Org" BorderColor="DarkBlue"
3 GridLines="Vertical" CellPadding="2" ShowFooter="False">
4 <EditItemStyle Font-Bold="True" ForeColor="White" BackColor="HotTrack"></EditItemStyle>
5 <AlternatingItemStyle BackColor="InactiveCaptionText"></AlternatingItemStyle>
6 <HeaderStyle HorizontalAlign="Center" Font-Bold="True" BackColor="Highlight"></HeaderStyle>
7 <Columns>
8 <asp:BoundColumn DataField="OFFICE" ReadOnly="True" HeaderText="Location">
9 <ItemStyle Width="15%"></ItemStyle>
10 </asp:BoundColumn>
11 <asp:BoundColumn Visible="False" DataField="Org" ReadOnly="True" HeaderText="Organization">
12 <ItemStyle Width="15%"></ItemStyle>
13 </asp:BoundColumn>
14 <asp:BoundColumn DataField="OfficeWinGoal" HeaderText="Office Win Goal" DataFormatString="{0:C}">
15 <ItemStyle HorizontalAlign="Right" Width="25%"></ItemStyle>
16 </asp:BoundColumn>
17 <asp:BoundColumn DataField="InsideBDBudgetHours" HeaderText="Inside Hours">
18 <ItemStyle HorizontalAlign="Right" Width="25%"></ItemStyle>
19 </asp:BoundColumn>
20 <asp:BoundColumn DataField="OutsideBDBudgetHours" HeaderText="Outside Hours">
21 <ItemStyle HorizontalAlign="Right" Width="25%"></ItemStyle>
22 </asp:BoundColumn>
23 <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit">
24 <ItemStyle HorizontalAlign="Right" Width="20%"></ItemStyle>
25 </asp:EditCommandColumn>
26 </Columns>
27 </asp:datagrid>
1 public void dgUpdateAnnual_UpdateOnClick(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
2 {
3 if (Page.Validate() = true)
4 {
5 // Get the current values
6 string strOrg = e.Item.Cells[1].Text;
7 TextBox txtTempGoal = (TextBox)e.Item.Cells[2].Controls[0];
8 TextBox txtTempInside = (TextBox)e.Item.Cells[3].Controls[0];
9 TextBox txtTempOutside = (TextBox)e.Item.Cells[4].Controls[0];
10 double dblOfficeWinGoal = Convert.ToDouble(txtTempGoal.Text);
11 double dblInsideBudgetHours = Convert.ToDouble(txtTempInside.Text);
12 double dblOutsideBudgetHours = Convert.ToDouble(txtTempOutside.Text);
13
14 dgUpdateAnnual.EditItemIndex = -1;
15 UpdateRecord(strOrg, dblOfficeWinGoal, dblInsideBudgetHours, dblOutsideBudgetHours);
16
17 // Put current values in Dataset and pass to the Command Object below
18 DataSet DS = new DataSet();
19 dgUpdateAnnual.DataSource = DS.Tables[ActualHours];
20 dgUpdateAnnual.DataBind();
21
22 bindData();
23 }
24 }
25
26 private void UpdateRecord(string strOrg, double dblOfficeWinGoal, double dblInsideBudgetHours, double dblOutsideBudgetHours)
27 {
28 String strQueryString = Request.QueryString["d"];
29 String strSQL = "UPDATE tblOpportunityBDYearly SET OfficeWinGoal = " + dblOfficeWinGoal.ToString() + ", InsideBDBudgetHours = " + dblInsideBudgetHours.ToString() + ", OutsideBDBudgetHours = " + dblOutsideBudgetHours.ToString() + " WHERE Org = '" + strOrg + "'";
30
31 Conn();
32 SqlCommand objCmd = new SqlCommand(strSQL, objConn);
33 objCmd.ExecuteNonQuery();
34 dConn();
35 }