Uncheck Checkboxes in GridView with Paging - c#

I have an ASP .NET GridView with Paging. One column in it has a CheckBox. In a certain scenario, I want to uncheck the checkboxes that are checked.
foreach (GridViewRow dr in gvMyGridView.Rows)
{
if (dr.RowType == DataControlRowType.DataRow)
{
if ((CheckBox)dr.FindControl("chkIsApplicable") != null)
{
((CheckBox)dr.FindControl("chkIsApplicable")).Checked = false;
}
}
}
But unfortunately because of Paging only the records that are currently shown in the Grid can be accessed in this way. I want it to apply to ALL the items in the GridView. This should happen client side and when the user commits will get saved to the database. Any way to handle this? :)

Maintaining the State of Checkbox while Paging in Gridview
The Logic
Save the checked rows primary keys to a list at PageIndexChanging event.
After setting the grid to new PageIndex and re-binding the grid, populate the new page with values in the list that is mapped to the rows in grid(if any)
So you will have a collection of checked rows in a list.
Delete the list to clear all.
This is one way of doing it.
PS: Its a two year old post, so you can surely optimise it with C# 4.0

Related

Asp.Net C# Dynamic GridView Options and Values on Sorting

I have a GridView Control that displays data from a table on Page_Load as well as a progress bar in one column and a button in another.
It then loops through each row of the Table and depending on the values, hides said button (i.e. if the 1st column value = "Open" then the button in the 8th column is hidden). It uses a simple foreach loop;
foreach (GridViewRow Row in MyGridView.Rows)
{
if(Row.Cells[0].Text == "Open")
{
Row.Cells[7].Text = "";
}
}
This is working absolutely fine on page load... unfortunately once the user sorts the data by the column values it doesn't work. I can get the event to fire (testing between OnSort and OnSorting amongst other events on the board) but it isn't actually making any changes to the table.
It seems the issues lays somewhere in how I'm attempting to initiate it... does anyone have any ideas?
Thanks in advance.
Managed to work this out through more testing. For anyone with a similar problem, use the DataGridViews OnPreRender Event.

Load checked datagridview row to another datagridview in another form

I have a DataGridView1 with data and a checkboxcolumn in the [0]
position.
In the form that contains the DataGridView1 with the checkboxes, I
have a button
Now, what I need is a way that allows me, after I check the rows I
want, get the checked rows to another form creating a small
datagridview with these rows.
I want to associate this function to the button_click event.
It's all about multiple selection, the basic "import the lines you want to a previous form".
Can anyone help me?
BIG THANK YOU
foreach (DataGridViewRow row in YourDgvMain.Rows)
{
if ((bool)row.Cells[0].Value == true)
{
YourOtherDgv.Rows.add(row);
}
}
My solution is a bit messy but should work: bind the row id to the checkbox, store the checked values in a collection, query again the datasource with the condition WHERE [id] IN ([your collection]) and create the second datagridview with the result of the query.
You should specify however what is the source for the gridview and how do you retrieve the data to get a better answer.

Gridview returning empty even when there is data. I want to use data in the gridview to update another database

I have a webform that the user captures data. This data affects other data that is in a Gridview. I wanted to right a DetailsView type control that makes an Update button visible if data is change in textboxes and drop downs that would require a master data update.
I have a gridview on the form and when the user click on update I wanted to update all the rows in the databases using the primary key OrderId which is on the Gridview.
If I put the code being each textbox and dropdown box and that calls a routine called DoUpdate then this works. However it is a little slow since each change forces an update, so I thought it would be better to rather have an update button appear, and then the user can click that to do the update.
When the routine is called from the button routine though the gridview has no rows. I assume I either need to findcontrol (gridview) or something but have not idea what to find it in.
Here is the code that works when called in a behind button event, but the gridview returns row count 0 when called as part of the code behind button:
List<string> _OrderIds = new List<string>();
foreach (GridViewRow gvr in gvOrderLines.Rows)
{
Label myOrderIDLablel = (Label)gvr.FindControl("lblOrderID"); //find control since it is a template field
_OrderIds.Add(myOrderIDLablel.Text);
}
The gridview is defined in the ASP page
<asp:GridView ID="gvOrderLines" runat="server" AutoGenerateColumns="False"
DataSourceID="odsOrderDetail"
Per my comment above you may want to do something like:
List<string> _OrderIds = new List<string>();
DataTable table = gvOrderLines.DataSource as DataTable;
foreach (GridViewRow gvr in table.Rows)
{
Label myOrderIDLablel = (Label)gvr.FindControl("lblOrderID"); //find control since it is a template field
_OrderIds.Add(myOrderIDLablel.Text);
}
I hope this get you headed in the right direction.

How do I mainain the state of checkbox?

I have a list of checkboxes in gridview.
You may check one or you may check as many as you wish, up to 10.
Once you check a box or boxes in a paging app, you page to the next page, the boxes are unchecked.
How do I keep the checked boxes to remain checked until a user unchecks them?
The code I have below isn't working.
For Each r As GridViewRow In GridMaks.Rows
If CType(r.Cells(0).FindControl("makos"), CheckBox).Checked Then
Dim marko As String = baseHint & "&cup=" & r.Cells(4).Text
CType(r.Cells(0).FindControl("marko"), CheckBox).Checked = True '// added this hoping to keep checkbox checked but it doesn't work
End If
Next
Sorry for including 2 flavors c# and vb.net. I can convert c# to vb if it helps.
If you're binding data to the grid each time a page loads, the checkboxes won't persist because they're being reset. Try performing a check like the simple sample code below to only bind data when the request is not a postback.
if (IsPostBack) {
// It is a postback, don't bind data
} else {
// It is not a postback, bind data
}
Also, you may find this article regarding Persisting checkbox state while paging helpful for what you need. In the GridView's "PageChanging" event, the article says to store the row index of each row that has a checked checkbox in a List, then store that List within the ViewState. When the page changes, retrieve that List from the ViewState and repopulate the GridView checkboxes.

Problem with dynamically added subheader into custom GridView

I'd like to get a grid with ability to add subheader (one or several rows) according to value of certain column (data source is grouped sorted by this column). Within my inherited GridView I override OnRowDataBind method: If value of considering column on current call of OnRowDataBind is not equal to previous values, then special header row is inserted before current row.
Table tbl = this.Controls[0] as Table; //this - pointer to CustomGridView
tbl.Controls.AddAt(rowIndex + add_counter + 1, NewSubHeaderRow); /*rowIndex - RowIndex of current row, add_counter - amount of already added SubHrader rows */
All works right. But problem occures on postback, when GridView restores it's state. First row and all its controls into every group (other words, first row after each dynamically added subheader row) comes without any attributes. But second row in group keeps both its own attributes and attributes of first row.
Some help will be very usefull. Maybe there is another (and right) way to add row into GridView. Then please, provide me with links to tutorials or articles.
Dynamic controls disappear on postback because on postback framework doesn't have information about such controls. It's programmer's responsibility to keep track of these dynamic controls and recreate them after postback...
Please refer to following post for re-creating dynamic controls on postback:
FAQ: Why do dynamic controls disappear on postback
Thanks for Waqas.
Here is my decision.
I override CreateChild(datasource, isBinding) and create list where put index of row, before which you should add subheader row. On SaveViewState i add this list to sealized object. On Load ViewState i load this list and if it's not null create subheader rows by saved indexes.
Maybe, there is a simpler and more natural way. But it's hidden from me securely )))

Categories

Resources