How to get value of TextBox of GridView in C#? - c#

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");
}

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();
}

Related

How to check if column header is clicked in datagridview using SelectionChanged event

I'm using SelectionChanged event of a DataGridView. I have a code that will display the value of a DataGridView's cell to a TextBox. However, when I click the column header, this also triggers the code (I assume because of the SelectionChanged event) and will display an error.
What I want to achieve is to enclose my codes in the datagridview1_SelectionChanged in an if statement. Wherein:
if(column header is clicked)
//don't do anything`
else
//do the display of data to textbox`
I just want to know the code for checking if you clicked a header or not.
just check in the event
List_SelectionChanged(object sender, EventArgs e)
{
if(e.Rowindex>-1)
...
}
-1 is for header
You could check your DataGridView's CurrentRow for null. If it is not null it means that you've not clicked the column header:
private void yourDataGridView_SelectionChanged(object sender, EventArgs e)
{
var current = yourDataGridView.CurrentRow;
if (current != null) // Means that you've not clicked the column header
{
//Display the value of a DataGridView's cell to a TextBox
}
}

How to get Row Id in RepositoryLookupEdit_ValueChanged event

I have gridcontrol that has a RepositoryLookupEdit in one of the columns. I can get the value of RepositoryLookupEdit after changed, but I dont know how to get the which row's RepositoryLookupEdit value changed. How can I get the Row ID?
With the code below, I can get the RepositoryLookupEdit value.
private void repositoryItemLookUpEdit1_EditValueChanged(object sender, EventArgs e)
{
LookUpEdit edit = sender as LookUpEdit;
var row = edit.Properties.GetDataSourceRowByKeyValue(edit.EditValue);
}
Since repositoryItemLookUpEdit isn't restricted to GridControls you cannot get the row handle from this event. You however have other possibilities.
First, if the edit is done by the user, you can use the ColumnView.GetFocusedRow() method to get the current grid row.
If however the edit value is changed via code it will also be changed in the grid so you can now use the ColumnView.CellValueChanged event.
private void repositoryItemLookUpEdit1_EditValueChanged(object sender, EventArgs e)
{
LookUpEdit edit = sender as LookUpEdit;
var row = edit.Properties.GetDataSourceRowByKeyValue(edit.EditValue);
gridRow = gridView.GetFocusedRow() as MyDataRow
}

How to hide a text box with a dynamic ID from another controller event handler?

I have an AddRow() method which generates a check box and a text box for each row in the table, I need to control the text visibility by checking the check box in the same row.
that seemed to be easy for the first while, since I can get the targeted text box ID but I dont know how to do it now.
here is my method:
private void AddRow(int nRowIndex, string strPaymentStatus,string PaymentRemark)
{
// checkBox -----------------------------------------------------------------
CheckBox objCheckBox = new CheckBox();
objCheckBox.ID = strBillID;
objCheckBox.Checked = false;
objCheckBox.CheckedChanged += new EventHandler(cbxPaymentStatus_CheckedChanged);
objCheckBox.AutoPostBack = true;
// textBox----------------------------------------------------------
TextBox objTbxRemark = new TextBox();
objTbxRemark.ID = BillID;
objTbxRemark.AutoPostBack = true;
}
CheckBox handler:
protected void cbxPaymentStatus_CheckedChanged(object sender, EventArgs e)
{
// here I can get the BillID (textBox ID) by a query, but I need to control its visibility from here
}
any help will be appreciated..
You can try to use
Page.FindControl("id")
If you can reconstruct the correct id. They probably follow some kind of pattern based on the parents controls
The you have to cast to TextBox
TextBox txt = Page.FindControl("id") as TextBox ;
txt.Visible =...

ModalPopup_Extender.Hide() Issue

I have a button, when clicked, a modalpopup with gridview inside it. as below;
protected void grdDetails_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = grdDetails.SelectedRow;
//Note: Value of that row's cell must display in A text box (txtBox) on main page
txtBox.Text = row.Cells[2].Text;
ChargeFilterModalDialogExtender.Hide();
}
After the extender is hide, the value of the cell not displaying in the text box. Am I doing something wrong?
Where you have the textbox?
Whether you are using UpdatePanel?, if so define the needed asynchronous postback triggers.

Catch dropdown selected text inside a repeater

I have dropdownlist inside a repeater and whenever the selected text is changed i have to show it in a textbox how can i do this??
protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
{
DropDownList ddl = (DropDownList)e.Item.FindControl("DropDownList6");
TextBox txt = (TextBox)e.Item.FindControl("TextBox4");
txt.Text = ddl.SelectedItem.Text;
}
First, don't use ItemCreated therefore since it triggered too early in the life-cycle(for the ViewState). You would also have to check for the ItemType first.
Instead use the DropDownLists SelectedIndexChanged event directly:
protected void Ddl_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList) sender;
RepeaterItem item = (RepeaterItem) ddl .NamingContainer;
TextBox txt = (TextBox) item.FindControl("TextBox4");
txt.Text = ddl.SelectedItem.Text;
}
you could add appropriate OnSelectedChange (somwthing) event handler to the DropDownList and then when event fired you catch it and do whatever you want , you can do it in both client side or server side .
You will need to use the add a handler to associate each dropdown control with the appropriate event handler. I don't have VS in front of me but it should be something like:
txt.SelectedIndexChanged += new EventHandler(YourMethodName)

Categories

Resources