Goal in mind, the concept is kind of like a shopping cart, so as they add items to list(Detail) it keeps the items they are adding in memory.
This works when ever I first load the list(grid) and add more rows. But if I set the first row and set the item and price and then decide to add 3 more rows then
the info I had added gets deleted instead of keeping its values and just load more lines to the list which would repopulate the gridview.
In the past I have done this with datatables but I want to be able to move from that and use this List Class
Also I have it set as viewstate so I can use it through out my page.
private ListArDocumentdetail Detail
{
get
{
ListArDocumentdetail _detail = new ListArDocumentdetail();
if (ViewState["Detail"] != null)
{
_detail = (ListArDocumentdetail)ViewState["Detail"];
}
return _detail;
}
set
{
ViewState["Detail"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
//creates 2 rows to start off
CreateRows(2);
}
public void CreateRows(int rowstoadd)
{
int newtotalrows = Detail.Count + rowstoadd - 1;
for (int i = Detail.Count; i <= newtotalrows; i++)
{
ArDocumentdetail detail = new ArDocumentdetail();
detail.Lineid = i;
detail.Itemid = 0;
detail.Quantity = 1;
if (Detail.Count > 0)
Detail.Insert(Detail.Count, detail);
else
Detail.Add(detail);
Detail = Detail;
}
gvInvoiceDetail.DataSource = Detail;
gvInvoiceDetail.DataBind();
GridViewRow row = gvInvoiceDetail.Rows[gvInvoiceDetail.Rows.Count - 1];
ImageButton btnAdd = (ImageButton)row.FindControl("btnAdd");
btnAdd.Visible = true;
}
protected void ibAdd_Click(object sender, ImageClickEventArgs e)
{
//user can type in how many rows they want to add on to current amount of rows
//so since grid starts off at 2 and they type 3 the grid refreshes with 5 rows.
CreateRows(Convert.ToInt32(txtRows.Text));
}
protected void UpdateRow(object sender, EventArgs e)
{
ImageButton btnUpdate = sender as ImageButton;
GridViewRow row = btnUpdate.NamingContainer as GridViewRow;
TextBox txtPrice = (TextBox)row.FindControl("txtPrice");
TextBox txtQuantity = (TextBox)row.FindControl("txtQuantity");
DropDownList ddlDescription = (DropDownList)row.FindControl("ddlDescription");
int index = Detail.FindIndex(f => f.Lineid == row.RowIndex);
Detail[index].Itemid = Convert.ToInt32(ddlDescription.SelectedValue);
Detail[index].Price = Convert.ToDecimal(txtPrice.Text);
Detail[index].Subtotal = Convert.ToDecimal(Detail[index].Price * Convert.ToInt32(txtQuantity.Text));
}
I can suggest you the logic:
Push a list into viewstate say Viewstate["List"],
Let a user chose an item. Then List list = (List)Viewstate["List"];
Add the selected item to List list. i.e. list.Add(item);
Now push the item back to viewstate. Viewstate["list"] = list;
Bind it to grid or display it on page. Whatever you want.
Related
I have a ListView on another form that I populate with this code
if (choice <= 1)
{
ListViewItem items = new ListViewItem(new[]
{
activity, statusType, blueprintName, runs, installerName, actTime, endDate, location
});
lstvw.Items.Add(items);
}
I change the data on the ListView with several different outputs using this code or similar for the other outputs.
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
listView1.Visible = false;
listView1.Items.Clear();
rbInt = 1;
choice = rbInt + cbInt;
listviewcolumnbuilder();
worker.IndustryReturns(choice, listView1);
this.Text = "Industry Jobs - All Jobs";
listView1.Visible = true;
}
I want to update a specific cell, in this case actTime, that is a DateTime that I convert to a string. That value specifically counts down to 0 on certain date.
What would be a good way to update just that specific cell automatically?
In my asp.net application, I have used Gridview control, In which i have to add Dropdownlist at runtime for each cell.Which i am able to bind successfully.
Below is my code which inside row databound event,
foreach (GridViewRow row in gdvLocation.Rows) {
if (row.RowType == DataControlRowType.DataRow) {
for (int i = 1; i < row.Cells.Count; i++) {
var dlRouteType = new DropDownList();
dlRouteType.ID = "ddlRouteType";
dlRouteType.DataSource = GetRouteTypeList();
dlRouteType.DataTextField = "RouteType";
dlRouteType.DataValueField = "Id";
dlRouteType.DataBind();
row.Cells[i].Controls.Add(dlRouteType);
}
}
}
I have a button in my page, which has functionality to save data to database . While saving data i have to pass the value from Dropdownlist which i have added at runtime. On button click i am writing following code to get data from dropdownlist,
var ddlDropDown = (DropDownList)row.Cells[i].FindControl("ddlRouteType");
But i am getting null in ddlDropDown object. I have even added Update panel inside aspx page. Any suggessions most welcome.
Thanks in advance
Sangeetha
You have these errors in your code
RowDataBound already iterates through each rows and so all you need not write that foreach on top
You are iterating from index 1, index are zero-based. So start from zero.
The DropDownList ID must be unique, so better write something like,dlRouteType.ID = "ddlRouteType_" + i;
The code should be,
protected void gdvLocation_RowDataBound(object sender, GridViewRowEventArgs e)
{
//removed the foreach loop
var row = e.Row;
if (row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < row.Cells.Count; i++) //changed index
{
var dlRouteType = new DropDownList();
dlRouteType.ID = "ddlRouteType_" + i; //gave unique id
dlRouteType.DataSource = GetRouteTypeList();
dlRouteType.DataTextField = "RouteType";
dlRouteType.DataValueField = "Id";
dlRouteType.DataBind();
row.Cells[i].Controls.Add(dlRouteType);
}
}
}
In DevExpress 13 GridControl.TableView with a dynamic list when a row is deleted the row selection doesn't dissapear. It remains on the row which replaced the deleted one. When the selected row is deleted, how can I make the row selection dissapear automatically too?
I tried to implement it through GridControl.BeginDataUpdate and GridControl.EndDataUpdate., dut it does not work.
private bool _isAlreadyLoaded = false;
private void GridControl_OnLoaded(object sender, RoutedEventArgs e)
{
if (ThisViewModel != null
&& _isAlreadyLoaded == false)
{
ThisViewModel.GettingNewRow += RefreshCommSessionsList;
_isAlreadyLoaded = true;
}
}
//InitializeDataList - method that getting List for GridControl.TableView
public void RefreshCommSessionsList()
{
App.Current.Dispatcher.Invoke(() =>
{
var a = GridControl.GetSelectedRowHandles();
int selectedRowHandle = -1;
if (a.Any())
{
selectedRowHandle = GridControl.View.FocusedRowHandle;
}
GridControl.BeginDataUpdate();
if (NewRowCount < 5 && ThisViewModel != null)
{
ThisViewModel.InitializeDataList();
TableView.DataControl.SelectItem(selectedRowHandle);
}
else
{
GridControl.RefreshData();
TableView.DataControl.SelectItem(selectedRowHandle);
}
GridControl.EndDataUpdate();
NewRowCount++;
});
}
Thank you!
You can use GridControl.CurrentItem property. If you set its value to null then the row selection will dissapear.
GridControl.CurrentItem = null;
To change the FocusedRow of a Grid, use the GridView!
So if your GridView is named the same as your GridControl it would go as follows,
GridView.FocusedRowChanged(null, null);
//Replace "GridView" with your GridView's name
I have search page which may return multiple search results in separate DataGrid controls, or if the search is specific enough, a single result in a single grid.
Should only one result be found, I then want to invoke the click of a ButtonColumn in the sole row of that grid to then open a separate page, as if the user clicked it themselves.
Here is my Page_LoadComplete event handler:
protected void Page_LoadComplete(object sender, EventArgs e)
{
var allControls = new List<DataGrid>();
// Grab a list of all DataGrid controls on the page.
GetControlList(Page.Controls, allControls);
var itemsFound = allControls.Sum(childControl => childControl.Items.Count);
for (var i = 0; i <= allControls.Count; i++)
{
itemsFound += allControls[i].Items.Count;
// If we're at the end of the for loop and only one row has
// been found, I want to get a reference to the ButtonColumn.
if (i == allControls.Count && itemsFound == 1)
{
var singletonDataGrid = allControls[i];
// **Here** I want to reference the ButtonColumn and then
// programmatically click it??
}
}
}
How can I obtain a reference to the ButtonColumn in question and then proceed to programmatically click it?
Found a solution. I programmatically invoke the OnSelectedIndexChanged event handler (defined as Select_Change) like so:
protected void Page_LoadComplete(object sender, EventArgs e)
{
var allControls = new List<DataGrid>();
// Grab a list of all DataGrid controls.
GetControlList(Page.Controls, allControls);
var itemsFound =
allControls.Sum(childControl => childControl.Items.Count);
for (var i = 0; i < allControls.Count; i++)
{
if (allControls.Count > 0 && allControls[i].ID == "grid")
{
// If a single row is found, grab a reference to the
// ButtonColumn in the associated grid.
if (i == (allControls.Count - 1) && itemsFound == 1)
{
var singletonDataGrid = allControls[i];
singletonDataGrid.SelectedIndex = 0;
Select_Change(singletonDataGrid, new EventArgs());
}
}
}
}
Using the reference to the DataGrid with the single row, I also set its SelectedIndex to the first (and only) row so that I can proceed with other operations after the invocation begins.
Is there a way in dropdownlist to show the desired number on pageload?
eg.
I have a dropdownlist control
I am using a for loop to populate it
for (int i = 1; i <= 100; i++)
{
DropDownList1.Items.Add(i.ToString());
}
Now this displays 1 on page load ... but I want to display 7..
How do I do that?
If you mean that it is the default selected value, you would just need to set a default selected value in the page load, after the list has been populated. Make sure only to do this when it is not a postback or you will overwrite any user selections.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.SelectedValue = "7"
}
}
Would set it inside your for loop. Would also store the default somewhere outside the code (db, config) so if it changes you don't have to redeploy.
if(!IsPostBack)
{
for (int i = 1; i <= 100; i++)
{
var newItem = new ListItem(i.ToString());
newItem.Selected = (i == 7);
DropDownList1.Items.Add(newItem);
}
}
After your for loop
DropDownList1.Items.FindByText("7").Selected = true;
Use the SelectedItem or SelectedIndex property.