ASP .Net Gridview - c#

I'm using a form to populate a gridview with x users. However I want whenever I call the function if there is only 1 user displayed inside the gridview to autoselect that first user.
So within my callback function I have
if (users.count == 1)
{
// Do something
}
I currently use the following function upon someone pushing "select" alongside the gridview.
Users_SelectedIndexChanged(object sender, EventArgs e)
It would be nice if I could reuse this function and do something like
if (users.count == 1)
{
Users_SelectedIndexChanged(object sender, EventArgs e);
}

Use the GridView.SelectedIndex property.
Setting this property to 0 after binding the data and checking for at least one item ought to do the trick. Technically you could call the event handler method, but to no end other than executing your own implementation.

I would use the GridView's OnLoad method and call your function there. Data has been bound when the OnLoad method is called.
public void GridView_OnLoad(object sender, EventArgs e)
{
//Assuming one row means 1 user and gv is your gridview object
if (gv.Rows.Count == 1) //(user.count == 1)
{
//call your selected function here
}
}

Related

Multiple DropDownList but same action

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
}
}

calling multiple dropdownlist index change methods within the another ddl index change

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.
}
}

sending data from one sublayout to another in sitecore

I'm having a hard time building a filtering system in Sitecore 7.
I have 2 sublayouts, on the same level of the page.
Sublayout A is a sidebar that contains a list of checkboxes and has an event that populates a List with the selected values.
Sublayout B displays a set of items.
What I would like to do, is send the populated List from sublayout A to sublayout B in order to filter the items list based on what the user selected.
I was able to do this by passing the data through Session, but this is not an optimal way of handling that data.
I've tried defining a property for sublayout A and loading the list there, but I can't get the exact instance of sublayout A from sublayout B in order to read the populated property.
Also, trying to Page.FindControl("IdOfSomeElementFromSublayoutA") always returns null in Sublayout B. Even though I've casted Page as the .aspx page that contains both Sublayouts.
I'm using Sitecore 7 Update 2.
Thanks a lot for your time.
The best way to do this is by raising (and subscribing to) events using the Sitecore.Events.Event class. Your sidebar sublayout would raise an event using something like the following within a button's click event handler:
Sitecore.Events.Event.RaiseEvent("YourEventName", new YourEventArgsClass { Property = "SomeValue" });
then in the other sublayout you would need to have the following set up in order to handle the event:
public partial class YourOtherSublayout : System.Web.UI.UserControl
{
private System.EventHandler eventHandlerRef;
protected void Page_Load(object sender, EventArgs e)
{
eventHandlerRef = EventHandlerMethod;
Sitecore.Events.Event.Subscribe("YourEventName", eventHandlerRef);
}
protected void Page_Unload(object sender, EventArgs e)
{
if (eventHandlerRef != null)
{
Sitecore.Events.Event.Unsubscribe("YourEventName", eventHandlerRef);
}
}
private void EventHandlerMethod(object sender, EventArgs e)
{
if (e != null)
{
//do stuff here
}
}
}
Note: it is important to keep the Page_Unload code there otherwise you will see the EventHandler method being called multiple times.

C#: DataGridViewCheckBoxCell strange behaviour [duplicate]

I have a DataGridView that loads data from a DataTable, along with an unbound column of DataGridViewCheckBoxCells. The rows in the DataGridView are compared with a separate DataTable with values the user has saved, and if there is a match, the checkbox for that row should check.
Here is the code that compares the values and sets the checkbox value to 'true':
foreach (int j in selectedObjectives)
{
foreach (DataGridViewRow r in dgvObjectives.Rows)
{
if (j == Convert.ToInt32(r.Cells["ObjectiveID"].Value))
{
dgvObjectives.CurrentCell = r.Cells["Select"];
((DataGridViewCheckBoxCell)r.Cells["Select"]).Value = true;
//dgvObjectives.InvalidateCell(r.Cells["Select"]);
//dgvObjectives.EndEdit();
//dgvObjectives.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
if (Convert.ToInt32(r.Cells["ObjectiveID"].Value) == selectedIndex)
{
r.Selected = true;
}
}
}
When I call the method to perform this action during the form load private void WQMDrill_Load(object sender, EventArgs e), the values are set correctly, but the checkboxes do not check. However, when called after the form is finished loading, the code works perfectly. Unfortunately for me, I absolutely need for these to check during the load process.
I hope I was clear with my issue, any help on this matter would be greatly appreciated. As you can see, I have tried to invalidate the cell alone, as well as the entire DataGridView control. I also have
private void dgvObjectives_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (this.dgvObjectives.CurrentCell.ColumnIndex == 0)
{
this.dgvObjectives.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
That doesn't fire during this time. Thank you.
You can put your checkbox selection and update logic in the DataBindingComplete eventhandler, this fires after the FormLoad but before anything is displayed to the user.
I'm not certain that calling CommitEdit will actually fire the Paint on the cell. Try handling the CellMouseUp event and firing EndEdit if the column is a checkbox column.
private void dgvObjectives_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
if (dgvObjectives.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn)
{
dgvObjectives.EndEdit();
}
}
I had the same problem, and tried a lot of different ways to deal with it, most failed, except when I tried this.BeginInvoke(new CDelegate()).

DataGridViewCheckBoxCell how to show checked when set during form load

I have a DataGridView that loads data from a DataTable, along with an unbound column of DataGridViewCheckBoxCells. The rows in the DataGridView are compared with a separate DataTable with values the user has saved, and if there is a match, the checkbox for that row should check.
Here is the code that compares the values and sets the checkbox value to 'true':
foreach (int j in selectedObjectives)
{
foreach (DataGridViewRow r in dgvObjectives.Rows)
{
if (j == Convert.ToInt32(r.Cells["ObjectiveID"].Value))
{
dgvObjectives.CurrentCell = r.Cells["Select"];
((DataGridViewCheckBoxCell)r.Cells["Select"]).Value = true;
//dgvObjectives.InvalidateCell(r.Cells["Select"]);
//dgvObjectives.EndEdit();
//dgvObjectives.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
if (Convert.ToInt32(r.Cells["ObjectiveID"].Value) == selectedIndex)
{
r.Selected = true;
}
}
}
When I call the method to perform this action during the form load private void WQMDrill_Load(object sender, EventArgs e), the values are set correctly, but the checkboxes do not check. However, when called after the form is finished loading, the code works perfectly. Unfortunately for me, I absolutely need for these to check during the load process.
I hope I was clear with my issue, any help on this matter would be greatly appreciated. As you can see, I have tried to invalidate the cell alone, as well as the entire DataGridView control. I also have
private void dgvObjectives_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (this.dgvObjectives.CurrentCell.ColumnIndex == 0)
{
this.dgvObjectives.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
That doesn't fire during this time. Thank you.
You can put your checkbox selection and update logic in the DataBindingComplete eventhandler, this fires after the FormLoad but before anything is displayed to the user.
I'm not certain that calling CommitEdit will actually fire the Paint on the cell. Try handling the CellMouseUp event and firing EndEdit if the column is a checkbox column.
private void dgvObjectives_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
if (dgvObjectives.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn)
{
dgvObjectives.EndEdit();
}
}
I had the same problem, and tried a lot of different ways to deal with it, most failed, except when I tried this.BeginInvoke(new CDelegate()).

Categories

Resources