my question is very simple. I have an AspxGridView with a DataTable as datasource.
Whene i filter rows by using the combox enabled by the ShowHeaderFilterButton property, the datasource is not updated and my row count remain the same.
How can i count the rows not hidden?
The ASPxGridView does not apply a filter condition on the underlying DataSource. So, after the filtering is done, the grid's DataSource has the same record count as it had before. A possible solution to this problem is to traverse through gridRows and getting required KeyField values or Row objects:
object[] rows = new object[ASPxGridView1.VisibleRowCount];
for(int i = 0; i < ASPxGridView1.VisibleRowCount; i++) {
rows[i] = ASPxGridView1.GetRowValues(i, ASPxGridView1.KeyFieldName);
//or
rows[i] = ASPxGridView1.GetRow(i);
}
Related
I am trying to insert column into gridview, I have dropdown below the grid and dropdown is having 10 items now If i select random item assume that I am selecting 8 number item and add clicking add button. this item should create as new column at 8 number in grid.
Suppose grid is not having any column when page is loading and trying to insert column using below code throwing error.
var boundField = new TemplateField { HeaderText = "SelectedProductName"};
grd.Columns.Insert(ddlProduct.SelectedIndex, boundField);
As Selected index can be any random number but grd is throwing out of index error as grid does not have that position.
Could you please help me to insert new column based on selected index from the dropdown.
My goal to achieve to display grid columns as they are listing into the dropdown same sequence.
First I defined a new class that will help us to store the columname and the index of that column in the dropdownlist:
public MyColumnClass
{
public MyColumnClass();
public int DDLindex {get;set;}
public string ColumnName{get;set;}
}
Here is the main code, each time a new column added, the following code will re-order them, based on their order in the dropdownlist:
BoundField boundField = new BoundField();
boundField.DataField = drp_Items.SelectedItem.Text;
boundField.Headertext = "Item";
myGridView.Columns.Insert(0, boundField);
//this list will store the existing
List<MyColumnClass> columnList = new List<MyColumnClass>();
for(int x = 0; x < myGridView.Columns.Count; x++)
{
//dropdownlist index of the column
var a = drp_Items.Items.IndexOf(drp_Items.Items.FindByValue(myGridView.Columns[x].DataField));
MyColumnClass column = new MyColumnClass();
column.DDLindex = a;
column.ColumnName= myGridView.Columns[x].DataField;
columnList.Add(column);
}
/the column list is created above, now we can clear the existing columns, and regenerate with the correct order
myGridView.Columns.Clear();
foreach(MyColumnClass item in columnList.OrderBy(c => c.DDLindex))//we order the list based on DDLindex
{
BoundField boundField = new BoundField();
boundField.DataField = item.ColumnName;
boundField.Headertext = "Item";
myGridView.Columns.Add(boundField);
}
P.S. You should make autogeneratecolumns="false" if you are binding data to Gridview.
Help me to sort the below issue.
I have a Dataset and the Dataset has Tables with 'n' number of rows.
I want to display first 5 rows from the table in dataset.
My Page will refresh every 1 minutes.So Whenever my pages is refreshing I need to display the next 5 rows from the dataset.
For Example
If my dataset has 15 rows.
When loading the page for first time it should display/get only first 5 rows (1 to 5)from the dataset.
Wehn the page postback/refresh it should dispaly second set of 5 rows (6 to 10) from the dataset.
And again when the page postback it should dispaly thirdset of 5 rows (11 to 15) from the dataset.
Note:I am not using GridView in my webPage.
I am using dynamic controls to populate the data's from the dataset.
Thanks,
David
You can clone the original DataTable, and copy whatever rows you want to the cloned DataTable.
private DataTable CopyDataTable(DataTable dt, int index)
{
DataTable cloneDT = dt.Clone(); // Clone DataTable Structure
for (int i = index, j = index + 5; i < j; i++)
{
DataRow dr = dt.Rows[i];
cloneDT.Rows.Add(dr.ItemArray);
}
return cloneDT;
}
You can invoke the method like this:
GridView1.Datasource = CopyDataTable(dt, 0); // create new DataTable, with rows 0-4
GridView1.DataBind();
GridView1.Datasource = CopyDataTable(dt, 5); // create new DataTable, with rows 5-9
GridView1.DataBind();
Set a hiddentfield and set the value as 1. then on pageload inside ispostback=true statement just increment the value of hiddenfield. then pass the value of hiddenfield into the db.
sample code...
if (isPostBack){
hiddentFieldForTimeCount.value= (int)hiddentFieldForTimeCount.value+1;
}
function CallDB(){
DataTable tbl=function callDB(hiddentFieldForTimeCount.value);
DataBind(tbl);
}
based on the hiddenfield value you can pick the data from database.
I've been looking for a way to control the text that appears in a particular column of a GridView.
For example consider a database with two tables Student and Class.
I want to add a column to the GridView which print out all the students in the Database, the column will show the student's class name, how can do it? (I can normally print the ClassId since its a FK in the student table)
I want to add a column to the GridView which print all the classes, the column will count the number of students in each class, how can I do it?
1- You can do that simply with inner join or a stored procedure to get the class name beside all the data you need in your query.
2- More than one way to do what you want:
For example:
you can add a column to your data table (empty column ), and fill it later through using Sum() aggregate function in a query.
DataTable result_dt = DAL_Helper.Return_DataTable(sqlSelect);//your original query
result_dt.Columns.Add("NumberOfStudent");
result_dt.Columns["NumberOfStudent"].DataType = typeof(string);
result_dt.Columns["NumberOfStudent"].MaxLength = 255;
if (result_dt.Rows.Count > 0)
{
for (int i = 0; i < result_dt.Rows.Count; i++)
{
//Here u can fill your new empty column.
}
}
result_dt.AcceptChanges();
After you return your customized data table(as a data source), you can bind it to the grid view.
Another solution : add an empty column to the grid view and in the RowDataBound event of the grid view you can fill this column through some loop and you can use LINQ to help you in getting the summation.
Its like I have multiselect option in Jqgrid in which i want to pass the selected rows value to the server and based on the value i will delete the rows. I dont want to have the ids to do my work. For the Single row i get the cell value and delete using the same. But for multi select its not the case. In getGridParam('selarrrow'); I use this to fetch the selected rows bu the values are not getting populated. Please help me out in doing the same
When i use the following code as i saw in some sample question i can fetch the value for the single row selection but when i select multiple rows then i pass it says like "FALSE" or "UNDEFINED". What can be the issue. var grid = jQuery('#list'); var sel_id = grid.jqGrid('getGridParam', 'selarrrow'); var myCellData = grid.jqGrid('getCell', sel_id, 'CountryId');
I got this done by using the for loop for getting the selected rows id
i did some thing like this and I am able to fetch the value. Thought this might help others too please check it
var myrow;
var id = jQuery("#List").jqGrid('getGridParam','selarrrow');
if(id.length)
{
for (var i=0;i<id.length;i++) // For Multiple Delete of row
{
myrow = jQuery("#List").jqGrid('getCell',id[i],'ticker');
}
}
I m using a dropdown to display "Location" field of a table. I want to set first item of dropdowm as "-Select Location-". I can't set tables first record as "Select" because table is stroed in xml format. And table file is generated dynamicaly.
I am currentaly using as
ddlLocationName.Dispose();
ddlLocationName.AppendDataBoundItems = true;
ddlLocationName.Items.Add("Select Location");
ddlLocationName.DataSource = _section.GetLocations();
ddlLocationName.DataBind();
ddlLocationName.AppendDataBoundItems = false;
but data is binded repeatedly.
What will be the solution for this problem?
Thaks in advance.
After you have databound, then call ddlLocationName.Items.Insert(0, "Select Location");
Example:
ddlLocationName.Items.Clear();
ddlLocationName.DataSource = _section.GetLocations();
ddlLocationName.DataBind();
ddlLocationName.Items.Insert(0, "Select Location"); // Adds the item in the first position
Access the items in the form of ListItems:
ListItem li = new ListItem("Select Location","-1");
ddlLocationName.Items.Add(li);
After you have bound your other data, use:
ddlLocationName.SelectedValue = "-1";
Also, you can add the values of your table in similar way to the ListItem first.