I have a method that gets a column value from an output gridview. How do I call this method in a button click event so that I can use the column value? Below is the method and button click event.
string mailAdd;
public void get_value(object sender, GridRecordEventArgs e)
{
mailAdd = e.Record["emailAddress"].ToString();
}
protected void btnsendMail_Click(object sender, EventArgs e)
{
//call get_value here
}
you cant ' because this method expect the EventArgs from the Sender which is the Grid object in your case , what I'd recommend is in
Get_value set the e.record["emailAddress"].toString() to a session and call in button
public void get_value(object sender, GridRecordEventArgs e)
{
Session["emailAddress"]= e.Record["emailAddress"].ToString();
}
protected void btnsendMail_Click(object sender, EventArgs e)
{
//you can use this
string _myEmail=Session["emailAddress"];
}
regards
is the part of the grid row? if yes then you should be using ItemCommand, if the button is separated from grid, then you may need to ascertain (dynamically) the row from which you want to read the email address. If you have the info, then you can easily access grid's content by row index and columnname, to get the email address, then rest is obvious.
Related
I have a entry/Textbox. And i have 10 number buttons as a calculator.(0-1-2-3-4....) When my page loaded entry having a number. Its showing me start value.( For example when my page loaded entry text being 10 already without any click to button) So i want to when i click a number button It will delete first entry value that loaded with page and it will start write my button clicks. How can I do it ?
I tried with:
private void btn1_Clicked(object sender, EventArgs e)
entry.Text="";
entry.Text="1";
But of course it didn't work because if i want to write 123 I cant write its always deleting. I just want delete first value which is coming with page loaded.
As #Jason suggested use the Placeholder property to display the initial value and in the click events append the value to the entry text:
Note: you can have a single event delegate that handle the click event for all buttons:
Constructor()
{
//set initial value here or use binding
//entry.Placeholder =
}
private void btn_Clicked(object sender, EventArgs e)
{
//you can remove the initial value if you want
//entry.Placeholder = string.Empty;
string buttonText = ((Button)sender).Text;
entry.Text = $"{entry.Text}{buttonText}";
}
An alternative way is to add a boolean flag out of the method to avoid this.
public class MyCalculator ()
{
private bool _isNewNumber;
protected override void OnAppearing()
{
_isNewNumber = true; //whenever open the page, needs to refresh
}
private void btn1_Clicked(object sender, EventArgs e)
{
if (_isNewNumber)
{
entry.Text=""; //clear the number for the first time
}
entry.Text="1";
_isNewNumber = false;
}
}
I have two DropDownList with OnSelectedIndexChanged="SelectedIndexChanged" but I need to know in the C# code withch one is the one I used.
How can I know that?
Answering the questions:
I'm using Web Forms and I'm trying to change some GridViews Source from a choseen option in a DDL but the web had the same DDL (with different IDs) in several places and I can't delete them...
The general form for an event handler is:
OnSomeEvent(object sender, EventArgs e)
sender is a reference to the object that is raising the event.
In your case, sender is a reference to the DropDownList whose selected index has changed. So you should use something like this:
private void SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList the_list_that_changed = (DropDownList)sender;
int ids = the_list_that_changed.SelectedIndex;
}
The first parameter sender represents the object raising the event. Hence the Sender reference to your DropDownList whose triggered the selected index changed.
private void SelectedIndexChanged(object sender, EventArgs e)
{
if (((DropDownList)sender).ID == "firstDropDownID")
{
//To Do for first dropdown
}
else
{
//To Do for second dropdown
}
}
I have a grid view that has a column called Amount .I have a textbox that shows the sum of amount column in gridview ,but i need to be aware when items in gridview are inserted changed and deleted because i need to update the textbox
Has anyone handled such a scenario. I would like to know which datagrid event I should be using,
I am using windows form application
Best regards
You can make use of RowsRemoved, RowsAdded and CellValueChanged events.
Updated
You can use these assuming your user is only adding and deleting 1 row at a time. also added a method with a linq statement to sum up your amount and assign it to the textbox.
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex.Equals(dataGridView1.Columns["Amount"].Index))
{
UpdateTotal();
}
}
private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
UpdateTotal();
}
private void dataGridView1_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
{
UpdateTotal();
}
private void UpdateTotal()
{
textBox1.Text = (dataGridView1.Rows.Cast<DataGridViewRow>()
.Sum(t => Convert.ToInt32(t.Cells["Amount"].Value))).ToString();
}
I am using visual studio 12, coding in asp.net using c#.
I have 3 dropdownlists in my code, all of which are bound by lists that I created.
I need some advise as to which method is better to call the postbackvalues of ddl's to perform a task.
Option 1
When a user selects an item from drop down list 3, the postbackvalue is sent from Dropdownlist3_SelectedIndexChanged to the dropdownlist2_selectedindexchanged by calling the method. Only after I have both the postbackvalues I would like to produce a chart. Regardless of what the chart holds and regardless of what the data is in the drop down list.
So something like
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
// I would like to have the postbackvalue of drop down list 3 here so i can use its value and dropdownlist2's postbackvalue to produce a chart.
}
and in the dropdownlist3
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
// I would like to call DropDownlist2_SelectedIndexChanged(...) method so I can send the postbackvalue of DDL3 for use in DDL2.
}
Option 2:
Define a global variable that stores the postbackvalue of Dropdownlist3 and use that value in Dropdownlist2_SelectedIndexChanged method for further use such as produces a chart.
I have read a lot about global variables but do not understand the con's about them.
I am not sure if this is what you are after, but perhaps having a third method which is called that handles the updating of the chart...
for example
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
BuildChart();
}
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
BuildChart();
}
private BuildChart()
{
var ddl3Value = DropDownList3.SelectedValue;
var ddl2Value = DropDownList2.SelectedValue;
if(ddl3Value != null && ddl2Value != null)
{
//build chart.
}
}
When I save a new item on datagridview the statement
MessageBox.Show(this.tb_aprovacao_admissaoDataGridView.CurrentRow.Cells[0].Value.ToString());
shows the value -1. How can I change it to show the real number of ID?
Thanks to everyone.
Depends how you want to get the value..
Do you want to get the value after you click on the cell, or on the click of a button or?
If you want to do it on the cell click event, you can do it like this:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
}
}
To get it with a button:
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(dataGridView1.CurrentCell.Value.ToString());
}