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"2Width="100%" Font-Names="Tahoma" BorderWidth="1px" DataKeyField="Org" BorderColor="DarkBlue"3GridLines="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>
1public void dgUpdateAnnual_UpdateOnClick(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)2{3if (Page.Validate() =true)4{5// Get the current values6string strOrg = e.Item.Cells[1].Text;7TextBox txtTempGoal = (TextBox)e.Item.Cells[2].Controls[0];8TextBox txtTempInside = (TextBox)e.Item.Cells[3].Controls[0];9TextBox txtTempOutside = (TextBox)e.Item.Cells[4].Controls[0];10double dblOfficeWinGoal = Convert.ToDouble(txtTempGoal.Text);11double dblInsideBudgetHours = Convert.ToDouble(txtTempInside.Text);12double dblOutsideBudgetHours = Convert.ToDouble(txtTempOutside.Text);1314dgUpdateAnnual.EditItemIndex = -1;15UpdateRecord(strOrg, dblOfficeWinGoal, dblInsideBudgetHours, dblOutsideBudgetHours);1617// Put current values in Dataset and pass to the Command Object below18DataSet DS =new DataSet();19dgUpdateAnnual.DataSource = DS.Tables[ActualHours];20dgUpdateAnnual.DataBind();2122bindData();23}24}2526private void UpdateRecord(string strOrg,double dblOfficeWinGoal,double dblInsideBudgetHours,double dblOutsideBudgetHours)27{28String strQueryString = Request.QueryString["d"];29String strSQL ="UPDATE tblOpportunityBDYearly SET OfficeWinGoal = " + dblOfficeWinGoal.ToString() +", InsideBDBudgetHours = " + dblInsideBudgetHours.ToString() +", OutsideBDBudgetHours = " + dblOutsideBudgetHours.ToString() +" WHERE Org = '" + strOrg +"'";3031Conn();32SqlCommand objCmd =new SqlCommand(strSQL, objConn);33objCmd.ExecuteNonQuery();34dConn();35}One way you can do that is by following how i wrote it below:
public void dgUpdateAnnual_UpdateOnClick(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e){double dblOfficeWinGoal = 0;double dblInsideBudgetHours = 0;double dblOutsideBudgetHours = 0;if (Page.Validate() =true) {// Get the current valuesstring strOrg = e.Item.Cells[1].Text; TextBox txtTempGoal = (TextBox)e.Item.Cells[2].Controls[0]; TextBox txtTempInside = (TextBox)e.Item.Cells[3].Controls[0]; TextBox txtTempOutside = (TextBox)e.Item.Cells[4].Controls[0];if (txtTemGoal.Text !="") { dblOfficeWinGoal = Convert.ToDouble(txtTempGoal.Text); }if (txtTempInside.Text !="") { dblInsideBudgetHours = Convert.ToDouble(txtTempInside.Text); }if (txtTempOutside.Text !="") { dblOutsideBudgetHours = Convert.ToDouble(txtTempOutside.Text); } dgUpdateAnnual.EditItemIndex = -1; UpdateRecord(strOrg, dblOfficeWinGoal, dblInsideBudgetHours, dblOutsideBudgetHours);// Put current values in Dataset and pass to the Command Object below DataSet DS =new DataSet(); dgUpdateAnnual.DataSource = DS.Tables[ActualHours]; dgUpdateAnnual.DataBind(); bindData(); } }Thanks for the reply. I tried doing something like this below that you wrote up, but it would throw an error - something about a bool value or expression not allowing a "". i even used "null" but it still errored out.?
Darmark:
if (txtTemGoal.Text !="")
{
dblOfficeWinGoal = Convert.ToDouble(txtTempGoal.Text);
}if (txtTempInside.Text !="")
{
dblInsideBudgetHours = Convert.ToDouble(txtTempInside.Text);
}if (txtTempOutside.Text !="")
{
dblOutsideBudgetHours = Convert.ToDouble(txtTempOutside.Text);
}
Hi,isheahan:
You can have a try with this statement.
if (String.IsNullOrEmpty(txtTemGoal.Text ))
No comments:
Post a Comment