Retrieving all GridViewRow objects from a GridView control with paging enabled - c#

I currently have a GridView control on my aspx page with paging enabled and I need to loop through the entire row collection/count to process the selected records. With my current code, it will only loop through the current page of GridView row.
What is the best way to accomplish this task?
Here is my current code:
ASPX page:
<asp:GridView ID="MyGridView" runat="server" AllowPaging="true" PageSize="20">
<Columns>
<!-- My Column list -->
</Columns>
</asp:GridView>
<asp:Button id="MyButton" runat="server" Text="Add" OnClick="MyButton_Click" />
code behind:
protected void MyButton_Click(object sender, EventArgs e)
{
for (int Count = 0; Count < MyGridView.Rows.Count; Count++)
{
//The row count is 20 and only contains the GridViewRow object in the current page view
//I want to retrieve the all GridViews rows so I can add them to a ReorderList control
}
}

Yes because your gridview UI is only aware of the current page.
Get the datasource and determine the row count from there...
int count = ((DataTable)MyGridView.DataSource).Rows.Count;
//or
int count = ((ICollection<SomeRecord>)MyGridView.DataSource).Count;

Simply use the following code:
//Change gridview to
GridView1.AllowPaging = false;
GridView1.DataBind();
//Transfer rows from GridView to table
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
{
for (int j = 0; j < GridView1.Rows[0].Cells.Count; j++)
{
//Add your code here..
}
}
}
//After filling your datatable change gridview paging style back to first, ie.
GridView1.AllowPaging = true;
GridView1.DataBind();
This may help you, let me know if this was helpful for you...

I think you should get the row count from your data source's row count.
If you need to filter rows, you can use DataTable's / DataView's Select method.
EDIT : You can not get actual row count by gridview.Rows.Count if gridview is paged. Depending on your comment, I assume that you're using listDataSource generic list to bind your gridview, you can get your row count as :
List<DataSourceItem> selectedRows =
listDataSource.FindAll(delegate(DataSourceItem item)
{
// Assuming you have a IsSelected bool property
// that refers your row is selected :
return item.IsSelected;
});
int rowCount = selectedRows.Count;

use session or state to store:
protected void Set_CheckboxStatus()
{
CheckBox selectall = (CheckBox)EmployeeGrid.HeaderRow.FindControl("gcb_selectall");
ArrayList cbstatuslist = new ArrayList();
if (Session["childcbstatus"] != null)
{
cbstatuslist = (ArrayList)Session["childcbstatus"];
}
foreach (GridViewRow row in EmployeeGrid.Rows)
{
int cb_index = (int)row.DataItemIndex; //For Getting DataItemIndex of EmployeeGrid
//int cb_index = (int)row.RowIndex;
CheckBox cb_selemp = (CheckBox)row.FindControl("gcb_selemp");
CheckBox cb_active = (CheckBox)row.FindControl("gcb_active");
if (cb_selemp.Checked == true)
{
if (!cbstatuslist.Contains(cb_index))
cbstatuslist.Add(cb_index);
}
else
{
cbstatuslist.Remove(cb_index);
}
}
Session["childcbstatus"] = cbstatuslist;
}
from the arraylist you can get all row index to loop and get the value from the gridview with paging.

#CRice's answer should have been the official answer.
Here is my solution. You need to presave the gridview's data, via its DataSource, into ViewState or Session.
GridView.Rows only refers to the "visible" rows, or the page currently shown on the screen.
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView gv = (GridView)sender;
DataSourceSelectArguments dss = new DataSourceSelectArguments();
//get the datasource related to the gridview
string wsDataSourceID = (gv.DataSourceID == string.Empty) ? ViewState["DataSourceID"].ToString() : gv.DataSourceID;
SqlDataSource sds = (SqlDataSource)pnlMAIN.FindControl(wsDataSourceID);
if (sds != null)
{
//load the data again but this time into a dataview object
DataView dv = (DataView)sds.Select(DataSourceSelectArguments.Empty);
if (dv != null)
{
//convert the dataview to a datatable so we can see the row(s)
DataTable dt = (DataTable)dv.ToTable();
if (dt != null)
{
//Save your data before changing pages
ViewState["AllTheData"] = dt;
gv.DataSource = dt;
gv.DataSourceID = null;
}
}
}
//now change pages!
gv.PageIndex = e.NewPageIndex;
gv.DataBind();
}
Next, when changing pages, here we save the data
protected void GridView_PageIndexChanged(object sender, EventArgs e)
{
GridView gv = (GridView)sender;
DataSourceSelectArguments dss = new DataSourceSelectArguments();
//reload the datatable back to the gridview
gv.DataSource = ViewState["AllTheData"];
gv.DataSourceID = null;
gv.DataBind();
I hope the code speaks for itself.
Thanks

Related

Button click pulls wrong DataTable for export into Excel

I'm pulling company schedule data from a web API and formatting the data so it can be displayed neatly on a local site and also available to export onto an excel template for distribution across the company.
I've added a button on the bottom of the site to display the previous weeks schedule and another to export the data to excel. When "Previous Week" is clicked it populates the same Gridview1 table with last weeks data. But When I click the export button it exports the data from the current week into Excel.
Here's some of my code, let me know what else I need to post for help.
The datatable gets set on page load into a gridview:
protected void Page_Load(object sender, EventArgs e)
{
// ..... bunch of code above to call web API for date range and format table....
DataView view = table.AsDataView();
view.Sort = "Sort ASC";
GridView1.DataSource = view;
GridView1.DataBind();
if (GridView1.Columns.Count > 0)
GridView1.Columns[8].Visible = false;
else
{
GridView1.HeaderRow.Cells[8].Visible = false;
foreach (GridViewRow gvr in GridView1.Rows)
{
gvr.Cells[8].Visible = false;
}
}
and the gridview gets pulled into a datatable and sent to an excel spreadsheet
protected void ExportExcel(object sender, EventArgs e)
{
DataTable dt = new DataTable("Resident Schedule");
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
dt.Columns.Add(cell.Text);
}
foreach (GridViewRow row in GridView1.Rows)
{
dt.Rows.Add();
for (int i = 0; i < row.Cells.Count; i++)
{
dt.Rows[dt.Rows.Count - 1][i] = row.Cells[i].Text;
}
}
dt.Columns.Remove("Rotation");
dt.Columns.Remove("Sort");
using (XLWorkbook wb = new XLWorkbook(#"C:\template.xlsx"))
{
IXLWorksheet ws = wb.Worksheet("Resident Schedule");
var rangeWithStrings = ws.Cell(5, 2).InsertTable(dt.AsEnumerable(), false);
// ..... bunch of code below
now I have a button click method that gets called to load the previous week into GridView:
protected void PreviousWeek(object sender, EventArgs e)
{
// ..... bunch of code above to pull and sort last weeks schedule....
DataView view = table.AsDataView();
view.Sort = "Sort ASC";
GridView1.DataSource = view;
GridView1.DataBind();
if (GridView1.Columns.Count > 0)
GridView1.Columns[8].Visible = false;
else
{
GridView1.HeaderRow.Cells[8].Visible = false;
foreach (GridViewRow gvr in GridView1.Rows)
{
gvr.Cells[8].Visible = false;
}
}
So, my question is, if the button click is repopulating GridView1 data, why is the ExportExcel method is going back to the Page Load data?
Click on button, send postback event and seems you missed to check whether request is postback or not. so whenever you click on button then always gridview populated with current week data. Check postback request and then poulate gridview on load event.
if (!IsPostBack)
{
// GridView Populate code
}

Make DataTable values as DropDown selected values in GridView

I have a datatable with values and I need to assign them as default values to gridview dropdown list on button click event. I have tried writing the below code and it retains only the last row values. How can I retain all the rows?
Am I missing to add rows to the GridView?
For example: DataTable now has three rows and in GridView only the third row has the default values from DataTable and the first rows are empty.
DataTable dtValues = dtSource;
if (dtValues.Rows.Count > 0)
{
for (int i = 1; i <= dtValues.Rows.Count; i++)
{
RadComboBox ID = (RadComboBox)gvGrid.Rows[i].Cells[1].FindControl("radID");
RadComboBox Names = (RadComboBox)gvGrid.Rows[i].Cells[2].FindControl("radName");
ID.SelectedValue = dtValues.Rows[i]["ID"].ToString();
Names.SelectedValue = dtValues.Rows[i]["Name"].ToString();
}
}
Can anyone please correct me if I'm doing something wrong?
You should need to use RowDataBound event of GridView as like below and populate it from DataTable as DataSource:
protected void gvGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
// check if row is not in edit mode
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataTable dtValues = dtSource;
// get dropdownlist from gridview
var ddl = e.Row.FindControl("YourDropDown") as DropDownList;
if (ddl != null)
{
ddl.DataSource = dtValues;
ddl.DataTextField = "Name"; // add text to dropdownlist
ddl.DataValueField = "ID"; // add value to dropdownlist
ddl.DataBind();
// add default selected value
ddl.Items.Insert(0, new ListItem("----- Select a Value -----", "0"));
}
}
}
Note: Don't forget to add OnRowDataBound event gvGrid_RowDataBound to your GridView.

How to get values from multiple DropDownLists in a GridView on button_click?

I have a GridView on my asp.net webform with all the columns and rows created in C# code from behind. Except the first column, every cell in each column is a DropDownList with a DataSource.
Here is my GridView:
<asp:GridView ID="gv_Rota" runat="server" AutoGenerateColumns="false" OnRowDataBound="gv_Rota_RowDataBound">
<HeaderStyle BackColor="#6a3d98" ForeColor="White" Height="20" />
<RowStyle HorizontalAlign="Center" Height="20px" Width="100px" />
<AlternatingRowStyle Height="20px" />
</asp:GridView>
And the c# creation of the columns and cells:
private void BindGrid(int Amount)
{
gv_Rota.DataSource = null;
gv_Rota.Columns.Clear();
BoundField bfield = new BoundField();
bfield.HeaderText = "Days";
bfield.DataField = "Days";
gv_Rota.Columns.Add(bfield);
for (int i = 0; i < Amount; i++)
{
int week = i + 1;
string sWeek = "Week " + week.ToString();
TemplateField tfield = new TemplateField();
tfield.HeaderText = sWeek;
gv_Rota.Columns.Add(tfield);
}
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Days", typeof(string)));
dt.Rows.Add("M");
dt.Rows.Add("T");
dt.Rows.Add("W");
dt.Rows.Add("T");
dt.Rows.Add("F");
dt.Rows.Add("S");
dt.Rows.Add("S");
gv_Rota.DataSource = dt;
gv_Rota.DataBind();
}
And then on the RowDataBound trigger of my GridView, I add the DropDownList control to every cell:
protected void gv_Rota_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 1; i <= ColumnCount; i++)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ddlShift = new DropDownList();
ddlShift.ID = "ddlShift";
ddlShift.DataSource = DCListOfShifts;
ddlShift.DataValueField = "SHIFT_ID";
ddlShift.DataTextField = "SHIFT_NAME";
ddlShift.DataBind();
ddlShift.Items.Insert(0, new ListItem("Shift..."));
ddlShift.CssClass = "ddl_rotamanager";
e.Row.Cells[i].Controls.Add(ddlShift);
}
}
}
What is displayed for example on my page:
So my question and problem is that I now want to save each column (except the first) to a database, as a "scheduled week", and have no idea how to get the values of selected items in all or any of the DropDownLists to then pass that back to the database. Baring in mind, this is NOT done on selectedindexchanged. I will select items from all the DropDownLists and then press a button to submit the values. Please could someone give me some highlight on how this could be done? Thanks.
EDIT 1: Response explains how I could get every value of a DropDownList in a row or by individual cell. However, now I need to know more specifically how I can get values of specific cells in the columns rather than the rows. Is it possible to get further highlight on this?
Use Gridview.Rows property
protected void MyButton_Click(object sender, EventArgs e)
{
foreach(GridViewRow row in gv_Rota.Rows)
{
//find this control in this row
DropDownList dd = row.FindControl("MyDropdown") as DropDownList;
//OR
//find this control in a specific cell
DropDownList day = row.Cells[0].FindControl("MyDropdown") as DropDownList;
DropDownList Week1 = row.Cells[1].FindControl("ddShift") as DropDownList;
//this is just a pseudo-code to determine which dropdown was selected per row.
//You can improve on this
if(dd1.SelectedValue != string.Empty)
thisDay.SelectedWeek = dd1.SelectedValue;
else if(dd2.SelectedValue != string.Empty)
thisDay.SelectedWeek = dd2.SelectedValue;
....
}
}

how to set row number in grid view

I need to set number of row in data grid view (like identity in sql) in winforms. i am dynamically adding columns and rows in grid view. any idea.
To display text in the row header you can use the Row.HeaderCell.Value as shown below:
void dataGridView1_DataBindingComplete(object sender,
DataGridViewBindingCompleteEventArgs e)
{
DataGridView gridView = sender as DataGridView;
if (null != gridView)
{
foreach (DataGridViewRow r in gridView.Rows)
{
gridView.Rows[r.Index].HeaderCell.Value =
(r.Index + 1).ToString();
}
}
}
This only displays the row number of the new row when the user begins typing in it. Not sure if there is a simple way of always showing the row number on the new row.
Another best way for asp.net application.Here is a link http://www.devcurry.com/2010/01/add-row-number-to-gridview.html
Just add the following tags to your section of your GridView
<Columns>
<asp:TemplateField HeaderText="RowNumber">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
...
</Columns>
Method 1:
//You can also use below code
this.DataGridView1.Rows[e.RowIndex].Cell[0].value =e.RowIndex +1;
//get total number of rows
this.DataGridView1.Rows.Count;
Method 2:
for (int i = 0; i < SensorGridView.Rows.Count; i++)
{
DataGridViewRowHeaderCell cell = SensorGridView.Rows[i].HeaderCell;
cell.Value = (i + 1).ToString();
SensorGridView.Rows[i].HeaderCell = cell;
}
Method 3:
void GridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
this.GridView.Rows[e.RowIndex].Cells[0].Value
= (e.RowIndex + 1).ToString();
}
Why not like this way. Build a datatable first and then bind to grid like below
DataTable tbl = new DataTable();
Datacolumn dc = new Datacolumn("ID");
Datacolumn dc1 = new Datacolumn("col1");
Datarow dr = tbl.NewRow();
dr[dc] = "123";
dr[dc1] = "SomeData";
tbl.Rows.Add(dr);
DataGridView.DataSource = tbl;

Postback resets the selectedValue of DropdownList inside GridView

My gridview has 2 columns - DropDownList & TextBox. The DDL is databound by a datatable. The SelectedIndex Change of DDL will populate its Textbox and also add a new row. All of this works fine, but the selected values of DDLs of previous rows, reset to 0 index, when a new row is added.The textbox values remain intact though. How can I retain the DDL selected values when adding new rows.
For eg. if I select 'abc' from dropdown of row 1, it populates its text field and also adds a new row. But with the postback happening, abc does not remain selected in row 1 ddl:
LOGIN PHONE
1 123456789
2
ASPX:
<asp:GridView ID="gvCommissions" runat="server"
OnRowDataBound="gvCommissions_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="LOGIN" ItemStyle-Width="29%">
<ItemTemplate>
<asp:DropDownList ID="ddlLogin" runat="server" Width="98%"
DataTextField="login" DataValueField="id"
OnSelectedIndexChanged="ddlLogin_SelectedIndexChanged" AutoPostBack="true" >
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PHONE" ItemStyle-Width="12%">
<ItemTemplate>
<asp:TextBox ID="txtPhone" runat="server" Width="98%"
Text='<%# Eval("phone")%>' Enabled="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
CODE BEHIND:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//Get values from DB, store to a datatable and then to a viewstate
GetAgentDetails();
//Adds an empty row to grid
SetInitialRow();
}
}
private void AddNewRow()
{
//Create a datatable
dtCurrentData = new DataTable();
dtCurrentData.Columns.Add("phone");
//Store values of each row to a new datarow. Add all rows to datatable
foreach (GridViewRow gvRow in gvCommissions.Rows)
{
DataRow drcurrentrow = dtCurrentData.NewRow();
drcurrentrow["phone"] = ((TextBox)gvRow.FindControl("txtphone")).Text;
dtCurrentData.Rows.Add(drcurrentrow);
}
//create an empty datarow and also add it to the new datatable.
DataRow dr = dtCurrentData.NewRow();
dr["phone"] = "";
dtCurrentData.Rows.Add(dr);
//Bind the new datatable to the grid
gvCommissions.DataSource = dtCurrentData;
gvCommissions.DataBind();
}
protected void gvCommissions_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlLogin");
dtAgents = (DataTable)ViewState["AgentsTable"];
ddl.DataSource = dtAgents;
ddl.DataBind();
ddl.Items.Insert(0, "");
}
}
When you databind in AddNewRow, the original rows are lost. You must store the dropdown selected index, together with your textbox phone value in dtCurrentData. You can then set the index of the dropdowns in your RowDataBound event, using the values your saved in dtCurrentData.
private void AddNewRow()
{
//Create a datatable
dtCurrentData = new DataTable();
dtCurrentData.Columns.Add("phone");
//new column for dropdown index
dtCurrentData.Columns.Add("ddlIndex");
//Store values of each row to a new datarow. Add all rows to datatable
foreach (GridViewRow gvRow in gvCommissions.Rows)
{
DataRow drcurrentrow = dtCurrentData.NewRow();
drcurrentrow["phone"] = ((TextBox)gvRow.FindControl("txtphone")).Text;
//get dropdown index
drcurrentrow["ddlIndex"] = ((DropDownList)gvRow.FindControl("ddlLogin")).SelectedIndex;
dtCurrentData.Rows.Add(drcurrentrow);
}
//create an empty datarow and also add it to the new datatable.
DataRow dr = dtCurrentData.NewRow();
dr["phone"] = "";
//initial drop down index
dr["ddlIndex"] = 0;
dtCurrentData.Rows.Add(dr);
//save to the viewstate like your AgentsTable
ViewState["CurrentData"] = dtCurrentData;
//Bind the new datatable to the grid
gvCommissions.DataSource = dtCurrentData;
gvCommissions.DataBind();
}
protected void gvCommissions_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlLogin");
dtAgents = (DataTable)ViewState["AgentsTable"];
ddl.DataSource = dtAgents;
ddl.DataBind();
ddl.Items.Insert(0, "");
//get the dropdown index from CurrentData
//use the current gridview's row index, since it matches the datatable
if (ViewState["CurrentData"] != null)
{
DataTable dtCurrentData = (DataTable)ViewState["CurrentData"];
ddl.SelectedIndex = Convert.ToInt32(dtCurrentData.Rows[e.Row.RowIndex]["ddlIndex"]);
}
}
}
RowDataBound gets fired every time (irrespective of whether the page is PostBack page or NOT).
You need to add that if(!IsPostBack) condition around the code where you are performing the databinding for the dropdowns in your RowDataBound event.

Categories