Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm little bit confusion, how to get value of TextBox of GridView in RowEditing and I've textchange event and that textbox value need to update via gridview to database. but before updating we need to calculate that value.

In RowUpdating we get value normally but in function calculationA() i'm not getting value of textbox. and need to calculate that value and show edited value in same textbox also.

public void calculationA()
{
    TextBox txt_BCICU = (TextBox)grdlist.FindControl("txt_BCICU");
    TextBox txt_BCSupDlx = (TextBox)grdlist.FindControl("txt_BCSupDlx");
    txt_TotalChargeA.Text = (Convert.ToDecimal(txt_BCSupDlx.Text.Trim()) + Convert.ToDecimal(txt_BCICU.Text.Trim())).ToString(); 

protected void txt_BCICU_TextChanged(object sender, EventArgs e)
    {
        calculationA();
    }     

protected void grdlist_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox txt_BCICU = (TextBox)grdlist.Rows[e.RowIndex].FindControl("txt_BCICU");
        TextBox txt_BCSupDlx = (TextBox)grdlist.Rows[e.RowIndex].FindControl("txt_BCSupDlx");
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
274 views
Welcome To Ask or Share your Answers For Others

1 Answer

APPROACH 1

You don't need TextChanged event to get value of textbox in gridview.

You can get the textbox value in RowUpdating event as in code below.

Also, remove the calculationA method and instead use the last line of code I have given in RowUpdating event.

protected void grdlist_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    string textBox1Text = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_BCICU")).Text;

    string textBox2Text = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_BCSupDlx")).Text;

    //remove the calculationA function and just use the code below in 
    //RowUpdating event
    txt_TotalChargeA.Text = (Convert.ToDecimal(textBox2Text.Trim()) + Convert.ToDecimal(textBox1Text.Trim())).ToString();
}

APPROACH 2

If you must have the TextChanged event then you can get textbox values as in code snippet below.

I have commented the call to calcualteA method since the same calculation can be done in TextChanged event. Note how the current grid row is obtained by getting the NamingContainer property of the textbox that raised the TextChanged event.

Get Textbox values in TextChanged event

protected void txt_BCICU_TextChanged(object sender, EventArgs e)
{

        //find this textbox Text i.e. txt_BCICU Text
        string txtBCICUText = (sender as TextBox).Text;


        //find the current grid row and through it other textboxes text
        GridViewRow currentRow = (sender as TextBox).NamingContainer as GridViewRow;

        //find textbox txt_BCSupDlx Text
        string txtBCSupDlxText = ((TextBox)currentRow.FindControl("txt_BCSupDlx")).Text;

       //do your calculation here
       txt_TotalChargeA.Text = (Convert.ToDecimal(txtBCSupDlxText.Trim()) + Convert.ToDecimal(txtBCICUText.Trim())).ToString(); 

        //calculationA();
}  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...