So, I have one GridView and 2 DataSources created using "wizard" ( GridViewTasks-->NewDataSource)
I have one CheckBox on my page...when checked=true I want to use DataSource1 for my GridView, and when checked=false DataSource2.
I have tried add CheckBox_CheckedChanged event in my code-behind something like this:
protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox.Checked == true)
{
gvPredbiljezbe.DataSource = dsGridView1;
gvPredbiljezbe.DataBind();
}
else
{
gvPredbiljezbe.DataSource = dsGridView2;
gvPredbiljezbe.DataBind();
}
}
But this doesn't work.
Any suggestions?
I know I can go in my code-behind and do it all "manually" (SqlDataConnection->DataAdapter->DataTable->GridViewDataSource) but is there a way when you create your DataSources with GridView wizard and on CheckBox or ButtonClick event change your GridView's DataSource's?
Thanks
Best
K
Make sure to set AutoPostBack="true" for your checkbox.
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox checkbox = (CheckBox)sender;
if (checkbox != null)
gvPredbiljezbe.DataSourceID = checkbox.Checked ? dsGridView1.ID : dsGridView2.ID;
}
I used to use this technique; don't assign the DatasourceID, then set it in page_init for the initial data load, then do:
protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox.Checked == true)
{
gvPredbiljezbe.DataSourceID = dsGridView1.ID;
gvPredbiljezbe.DataBind();
}
else
{
gvPredbiljezbe.DataSourceID = dsGridView2.ID;
gvPredbiljezbe.DataBind();
}
}
Something like that; swapping the ID of the datasource worked without issue (been a little while so I think you use ID, not ClientID property).
Related
I have a Gridview that is populated through SQLDataSource. The query behind is rather complex and the GridView takes some seconds to get filled; that's why I get annoyed by the fact that every time I select a row, the Gridview disappears for a while and is getting repopulated again. What does fire that rebind?
The selected row index works as Control Parameter for a second Gridview, that displays detail information on that row. There are these 2 events defined for the gridview:
protected void GridView_PURCHTABLE_OnDataBound(object sender, EventArgs e) {
if(DisplayPurchItems.Checked == false)
{
GridView_PURCHTABLE.Columns[4].Visible = false;
}
else
{
GridView_PURCHTABLE.Columns[4].Visible = true;
}
protected void GridView_PURCHTABLE_Selectedindexchanged(Object sender, EventArgs e) {
GridView_Notes.DataBind(); //this is the second gridview
}
Anyone has a clue what might cause the gridview to rebind?
Martin
Check once:Is post back.
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//binding grid
}
}
I have two files in my project. One is user control (popup) customerpicker.ascx and one is default.aspx page. In customerpicker I have dynamically generated gridview and 'select' column with SelectButton.
What I want is this: When I click on 'select' on random row in gridview, then I like to display value from selected row immediately (like ajax) to aspx.page. How it is possible?
There is part of my code in .ascx:
public string showOnaspx { get; set; }
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
showOnaspx = row.Cells[1].Text;
e.Cancel = true;
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
}
I tried casting, get/set methods, but nothing worked for me.
Answer on that question is pretty easy.. We can save variabile in session and then read it in aspx.page wherever we want.
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
Session["sessionid"]= row.Cells[1].Text;
e.Cancel = true;
}
Feeling so stupid right now, because I spent few days to figure it out simple way to do that.
Working in VS 2012, WinForms, C#...
I have a ListBox I would like to populate depending upon the value selected in a ComboBox. I've tested my SQL Query and it works, but I'm getting a weird problem where, when I run my routines, my ComboBox comes up empty, as well as my ListBox. When I comment out the code in my cb_Session_SelectedValueChanged routine, my CB and LB load just fine, but when it's not commented out is when my LB and CB end up blank.
This is what I have:
private void cb_Session_SelectedValueChanged(object sender, EventArgs e)
{
listbox_Sessions.Visible = true;
LoadSessionListbox();
}
private void LoadSessionListbox()
{
int tempID = Convert.ToInt32(cb_Session.SelectedValue);
// Code here to load listbox, which works without above routine.
}
Am I missing something? Why are my CB and LB blank with that first routine added?
[EDIT]:
I put the routines which were in SelectedValueChanged in a MouseClick event and it works, but not when I want it to... You have to click a couple times to get it to re-load with the correct ID. I feel like I'm getting closer, but still not the right event.
Try this:
private void cb_Session_SelectedValueChanged(object sender, EventArgs e)
{
if(cb_Session.SelectedValue>-1)
{
listbox_Sessions.Visible = true;
LoadSessionListbox();
}
}
Figured it out!!
I ended up adding a simple if statement to my SelectedValueChanged routine, and it fixed everything!
private void cb_Sessions_SelectedValueChanged(object sender, EventArgs e)
{
listBox_Sessions.Visible = true;
if (cb_Sessions.SelectedValue != null)
LoadSessionListbox();
}
Works perfectly now.
Try in SelectedIndexChanged Event and follow
private void cb_Session_SelectedIndexChanged(object sender, EventArgs e)
{
if (cb_Session.SelectedValue == null) return;
if (cb_Session.SelectedIndex == -1) return;
listbox_Sessions.Visible = true;
LoadSessionListbox((int)cb_Session.SelectedValue);
}
private void LoadSessionListbox(int selectedValue)
{
//TODO: Do stuff
}
I have a gridview with 2 column, one is a DataGridViewCheckBoxColumn named "choose" , the other is an ordinary DataGridViewTextBoxColumn named "ID"... I want to change the text of a textbox immediately when the checked of chechkbox column changed .... but i don't know which event should i use ....
void SetTextBox()
{
TextBox1.Text="";
for (int i = 0; i < MyGrid.Rows.Count; i++)
if (Convert.ToBoolean(MyGrid.Rows[i].Cells["choose"].Value) == true)
{
TextBox1.Text += MyGrid.Rows[i].Cells["ID"].Value.ToString()+",";
}
}
private void !!!!which Event?!!!!(object sender, EventArgs e)
{
SetTextBox();
}
There's actually two events you need to deal with. Here's an example adapted from the code I'm working on right now.
private void dgvRMADetail_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dgvRMADetail.CurrentCell is DataGridViewCheckBoxCell))
{
dgvRMADetail.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
private void dgvRMADetail_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == useRowCheckboxColumn.Index)
{
// Logic for doing whatever when the checkbox is checked.
}
}
The first event tells it "Automatically commit this when it changes", the second event is "When the value is committed, do something".
I have a dropdown list and radio button. If something is selected from the dropdown by the user, I want the radio button cleared. If the radio button is selected I want the selection of the dropdown cleared. Unfortunately, this creates events that cancel each other out. I tried using the sender as shown below to determine if the value was being changed by code or by the user, but that doesn't work. How do I make these events only work if the user is the source of the action?
private void rbBlank_Checked(object sender, RoutedEventArgs e)
{
// Verify source of event
if (sender is RadioButton)
{
// Display
comboBoxTitles.SelectedIndex = -1;
}
}
private void comboBoxTitles_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
// Verify source of event
if (sender is ComboBox)
{
// Display
rbBlank.IsChecked = false;
}
}
You won't be able to tell the difference between the two since the source will be the same instance for both occasions.
This doesn't answer the question directly but if you compare the SelectedIndex of comboBoxTitles in the SelectionChanged event handler, your problem should be solved
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (comboBoxTitles.SelectedIndex != -1)
{
rbBlank.IsChecked = false;
}
}
Try to compare if sender == instance of a control instead of is type of.
private void rbBlank_Checked(object sender, RoutedEventArgs e)
{
// Verify source of event
if (sender == rbBlank)
{
// Display
comboBoxTitles.SelectedIndex = -1;
}
}
private void comboBoxTitles_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
// Verify source of event
if (sender == comboBoxTitles)
{
// Display
rbBlank.IsChecked = false;
}
}
If you know the IDs of those controls, you can try something like this:
System.Web.UI.WebControls.WebControl webControl = (System.Web.UI.WebControls.WebControl) sender;
if( webControl.ID == <comboboxId>)
{
//Do something
}
I havent tried this, but I guess it might work.