Inserting Gridview Column from code behind based on random index in asp.net - asp.net

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.

Related

How to add values to DataGridColumn dynamically in ASP.net

In the project I am working in there is webpage which retrieve data from the database. So I have used DataGrid show my data.
But sometimes I wanted to add more columns to the DataGrid.That means I want to add some columns with data dynamically.
However I have new columns addition to the DataGrid and here is my code.
DataGridColumn myCol = new BoundColumn();
myCol.HeaderText = "Test";
myCol.Visible = true;
grdHeiskort.Columns.Add(myCol);
In here "grdHeiskort" is DataGrid ID.Now I want to add the data to this column named="test".That data is gain from the database.Suppose I am retrieving dataset from DB and I want to add the data in 1st column in dataset to my "test" column.So how can I do it.
If you use a BoundColumn you can set the name of the DataField to whatever the name of the field you want it to be from your dataset:
BoundColumn myCol = new BoundColumn();
myCol.HeaderText = "Test";
myCol.Visible = true;
myCol.DataField = "NameOfYourDataColumn";
grdHeiskort.Columns.Add(myCol);

Item add in bottom of drop down list with value field

how i can add item in bottom whenever i am bind the gridview dynamic.
and i want a value field. because i have a search criteria with drop down list.
With Drop Down List Value I am sending the value with drop down list .
SqlParameter[] param3 = new SqlParameter[] { new SqlParameter("#status","org")
};
ddlDistrictCode.DataSource = comnFunctionObj.getDropDownList(clsConstant.GET_ALL_DROPDOWN, param3);
ddlDistrictCode.DataTextField = "DisplayFieldText";
ddlDistrictCode.DataValueField = "ValueFieldText";
ddlDistrictCode.DataBind();
Search Criteria
By this I am sending the value
new SqlParameter("#DISTRICTCODE",Convert.ToInt32((ddlDistrictCode.SelectedValue))),
After you databind, just add the value manually:
ddlDistrictCode.Items.Insert(lst.Items.Count - 1, new ListItem("VALUE", "TEXT"));

How to get Set 5 rows from dataset in every PostBack in asp.net

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.

AspxGridView and ShowHeaderFilterButton property

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

How to set dropdown's firsl Item -Select- and remaining Items from a column of table?

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.

Resources