Editing a gridview by a personal Keyboard - c#

I did a gridview with a datatable as a datasource. I did also a form for a keyboard that write on a textbox.
I call the keyboard in this way:
frmKeyboard k = new frmKeyboard(myTextBox);
When the keys on the keyboard are pressed, the character are insert on the textbox. But for the gridview it is different. I can't pass a textbox, but i know the gridview is in "textedit" as editing.
I call the form in this way:
private void gridSelfLearning_ShownEditor(object sender, EventArgs e)
{
GridView self = sender as GridView;
TextBox prova = null;
if(self.ActiveEditor is TextEdit)
{
frmKeyboard k = new frmKeyboard(prova);
k.ShowDialog();
}
}

Related

Addressing DatagridView Datasource from another object trigger event

I am programming an interactive datagridview on WPF, the user can click on cells causing the content to change via selecting from multiple data sources. When the user click a cell the double click cell event is triggered like:
private TabItem item(..){
Table= new forms.DataGridView();
Table.DataSource = DataSource1(); //Default
Table.CellDoubleClick += delegate (object sender, forms.DataGridViewCellEventArgs args){
{
var senderGrid = (forms.DataGridView)sender;
//Depending on some conditions the user can switch to DataSource2(), DataSource3()
if(Condition 1)
senderGrid.DataSource = DataSource2();
}
Up until this point, everything works as intended ! The table double click event switches data sources as programmed.
Now the issue occurs, when I want to update the table from a Combo Box event trigger. The table remains simply unchanged, I guess I am not addressing the object correctly.
//Still inside the same TabItem item(..)
newCombo.DropDownClosed += delegate (Object sender, EventArgs e)
{
ComboBox cmb = sender as ComboBox;
if(Condition)
Table.DataSource = DataSource2();
}
Is there a way to control the table outside its event functions ?

Change Value of a Dynamically Created Text Box when button is clicked C#

I have dynamically created a button using a function I made which is meant to increase the value of another button created using a similar function. I'm able to retrieve the name of the dynamically created textbox, but since it is dynamically created, I can't reference it.
I've tried to pass the textbox through as a parameter, but it doesn't appear that I'm able to pass extra parameters:
Button ButtonDecreaseValue= addButton(ItemName, "-");
TextBox TextBoxItemAmount = addTextBox(ItemName, "0");
So, how would I change the value of textbox one when ButtonRemoveItem is clicked?
I've created an event handler for the button, like so:
ButtonIncreaseValue.Click += new EventHandler(ItemDecreased);
But, inside the event handler, I'm unsure how I would change the textbox name.
private void ItemDecreased(object sender, EventArgs e)
{
Button currentItem = (Button)sender;
int ItemNo = Convert.ToInt32(currentItem.Tag);
DataRow ItemInfo = getItemDetails(ItemNo);
ItemInfo[0].ToString();
}
When clicking the button, the corresponding textbox should decrease in value.
Assuming this is Windows Forms
You said you have the name of the TextBox? You should be able to find it with that.
private void ItemDecreased(object sender, EventArgs e)
{
string textBoxName = HoweverYouGetTheDynamicTextBoxName();
// Assumptions:
// 1. ItemDecreased is a method on a Form or a UserControl.
// 2. There is only one TextBox with the given name in the Form or UserControl.
// 3. The TextBox is added to the Form or UserControl's Controls property.
TextBox tb = Controls.Find(textBoxName, true).OfType<TextBox>().SingleOrDefault();
if (tb is null)
{
// Couldn't find the text box for some reason.
}
// tb references the dynamically created text box.
}

How to get value of TextBox of GridView in 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();
}

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.

Categories

Resources