Validating data in BoundColumn field

Last post 12-21-2006 1:26 AM by rexlin. 3 replies.

Sort Posts:

  • Validating data in BoundColumn field

    12-20-2006, 4:10 PM
    • Loading...
    • isheahan
    • Joined on 05-11-2005, 4:58 AM
    • Posts 125

    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   		}
    
     
     
  • Re: Validating data in BoundColumn field

    12-20-2006, 4:35 PM
    • Loading...
    • Darmark
    • Joined on 09-11-2003, 3:09 PM
    • Long Island, NY
    • Posts 529
    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 values
           	string 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();
         	}
     }
    
    
     
     
    Darmark

    Mark as Answer, if this reply answers your post.
  • Re: Validating data in BoundColumn field

    12-20-2006, 4:41 PM
    • Loading...
    • isheahan
    • Joined on 05-11-2005, 4:58 AM
    • Posts 125

    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);
    }
     
  • Re: Validating data in BoundColumn field

    12-21-2006, 1:26 AM
    Answer
    • Loading...
    • rexlin
    • Joined on 07-17-2006, 4:43 AM
    • Posts 1,751

    Hi, isheahan:

    You can have a try with this statement.

    if (String.IsNullOrEmpty(txtTemGoal.Text ))



    Best Regards,
    __________________________________________________
    Sincerely,
    Rex Lin
    Microsoft Online Community Support

    This posting is provided "AS IS" with on warranties, and confers no rights.
Page 1 of 1 (4 items)
Microsoft Communities
Page view counter