I have two pre populated combo boxes with value = “ID” and Text = Relevant data...
I am doing an edit in a grid view and I adjusted the template to have the two combo boxes (each relevant field has its own).
I have searched high and low and I assume this is so easy people don’t even ask about it... but if you see below the combo boxes do not populate the parameters with the value of the combo boxes.
So I have tried this:
protected void aspxGridviewUsers_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
{
e.NewValues["StoreID"] = GetStoreID();
e.NewValues["GroupID"] = GetGroupID();
}
protected int GetStoreID()
{
ASPxComboBox CBID = new ASPxComboBox();
CBID = aspxGridviewUsers.FindEditFormTemplateControl("ASPxComboBoxStoreEdit") as ASPxComboBox;
return Convert.ToInt32(CBID.SelectedItem.Value.ToString());
}
protected int GetGroupID()
{
ASPxComboBox CBID = new ASPxComboBox();
CBID = aspxGridviewUsers.FindEditFormTemplateControl("ASPxComboBoxGroupEdit") as ASPxComboBox;
return Convert.ToInt32(CBID.SelectedItem.Value.ToString());
}
This did not work. What am I doing wrong here?
How can I use those values in the comboboxes to update against my update statement?
I would suggest that you use the following code:
return Convert.ToInt32(CBID.Value);
code. Does it work for you?
Related
I'm using a base class to modify the behavior of any Telerik RadGrid that appears on my ASP.Net pages. In the base class, I want to perform certain operations (set Css, tool tips, etc) on many common columns, but not every common column exists in every grid.
In the ItemDataBound event I'm getting an instance to the GridDataItem and in turn I want to get a reference to one or more of the contained cells of the GridDataItem:
var cell = gridDataItem["ColumnUniqueName"]
Problem is that this throws a GridException if the named column doesn't exist:
Cannot find a cell bound to column name 'ColumnUniqueName'
Is there a way to test for existence of a column by name before referencing it or am I
stuck using try catch?
Will sent me on the right path:
var tableView = gridDataItem.OwnerTableView;
var gridColumn = tableView.Columns.FindByUniqueNameSafe(uniqueName);
if (gridColumn != null)
{
var cell = gridDataItem[gridColumn];
...
Try using the RenderColumns collection:
protected void rgGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
bool found = (from d in rgGrid.MasterTableView.RenderColumns select d).Any(d => d.UniqueName == "ColumnUniqueName");
}
}
I need your help please.
I have this method:
protected void btnDocumentType_Click(object sender, EventArgs e)
{
DocumentApplicationCategoryManager DACM = new DocumentApplicationCategoryManager();
IkubInfo.NE.Domain.DocumentApplicationCategory DAC = new Domain.DocumentApplicationCategory();
DAC.DocumentType = new DocumentTypeManager().GetById(new Guid(cboDocumentType.SelectedValue));
DAC.ApplicationCategory = Entity;
Entity.DocumentApplicationCategory.Add(DAC);
DACM.Save(DAC);
DACM.Session.CommitChanges();
SetUIValues();
}
This is the method for the ADD button, which allows the user to add a value in the grid.
I need to check that if the value that the user is trying to add is saved once, it cannot be saved twice. I need to validate it and show user an error message but I don't know how to do this.
I guess that I have to put an "if" condition before this line:
DAC.DocumentType = new DocumentTypeManager().GetById(new Guid(cboDocumentType.SelectedValue));
Any idea ?
Your help would be appreciated.
Thanks in advance :)
protected void btnDocumentType_Click(object sender, EventArgs e)
{
DocumentApplicationCategoryManager DACM = new DocumentApplicationCategoryManager();
IkubInfo.NE.Domain.DocumentApplicationCategory DAC = new Domain.DocumentApplicationCategory();
DAC.DocumentType = new DocumentTypeManager().GetById(new Guid(cboDocumentType.SelectedValue));
DAC.ApplicationCategory = Entity;
//Check here from DocumentApplicationCategory, Whether DAC.DocumentType and Entity Exists or not, if does not exists then allow to come in
if(CHECK_HERE)
{
Entity.DocumentApplicationCategory.Add(DAC);
DACM.Save(DAC);
DACM.Session.CommitChanges();
}
SetUIValues();
}
refer the comments: at place of CHECK_HERE, Put your condition to confirm the data your are trying to insert already exists or not.
What you need to do is to check for every value in the combobox if the value inserted exists or not. To do so, you need to create a loop with a condition of type HasNext or IsNull and inside th loop you will use your If statement that will compare the values of the ID (from what i have understood from your code) .
I'm trying to figure out how to sort a GridView with multiple columns (String, DateTime, Decimal, etc. data-types) which is bound to a generic list of custom objects.
MyObject.vb:
Public Property Id
Public Property Name
Public Property Date
Public Property Amount
MyObjects.aspx.vb:
gridView.DataSource = GetMyObjects()
gridView.DataBind()
Note: GetMyObjects() returns a List of MyObject
Basically, I need to be able to click on the column headers of the grid to sort and reverse sort, and also be able to store the sort direction in ViewState so the direction persists each time I click on a column header.
It seems like I probably need MyObject to implement IComparable, but I'm not sure quite how to put it all together.
Can anyone suggest a good tutorial for this, or point me in the right direction?
You need to enable sorting (AllowSorting) and handle the event OnSorting.
Note: The sample code uses C#, but the VB version should be similar.
Create your GridView:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" OnSorting="GridView1_Sorting">
</asp:GridView>
Handle OnSorting:
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
GridView1.DataSource = GetObjects(e.SortDirection, e.SortExpression);
GridView1.DataBind();
}
GetObjects returns a sorted List<MyObject>. You have to create your own sorting logic here, one alternative could be using Dynamic Linq. If you choose that route, GetObjects could be defined like this: (there are better ways, but this is enough to show the theory)
private List<MyObject> GetObjects(SortDirection sd, string se)
{
// Have we generated data before?
if (SimulatedDB == null)
{
// Create a sample DB
SimulatedDB = new List<MyObject>();
var rnd = new Random();
for (int i = 0; i < 20; i++)
{
var node = new MyObject();
node.Id = i;
node.Name = String.Format("Name {0}", i);
node.CreationDate = DateTime.Now.AddDays(rnd.Next(100));
node.Amount = (rnd.Next(1000) * rnd.NextDouble());
SimulatedDB.Add(node);
}
}
// Return sorted list
if (sd == SortDirection.Ascending)
return SimulatedDB.AsQueryable<MyObject>().OrderBy<MyObject>(se).ToList();
else
return SimulatedDB.AsQueryable<MyObject>().OrderByDescending<MyObject>(se).ToList();
}
Hope it helps.
I am playing about just now trying to teach myself a little bit about the entity framework.
I have a Gridview data bound to a Entity Date Source using the Entity Framework. If I select certain items in that list I then wish to redirect another page and populate another gridview with just the items selected (but with more detail, different includes/navigation properties)
This is probably the most simple thing but I have spent 2 hours banging my head on the wall trying to get this to work.
Essentially I have a continue button which when clicked should identify all the UIDs (a column in the gridview) of the rows and allow me to subset to just these rows and pass them to another page to be rebound to another datagrid
Any ideas???
Well, the big picture is that you should get those IDs, pass them to the other page, and then use a query with Contains; see this question for an idea of how to use it:
How search LINQ with many parametrs in one column?
Assuming you haven't used DataKeys in your GridView, this would be my approach.
Page 1
protected void Button1_Click(object sender, EventArgs e)
{
var checkedItems = new List<int>();
foreach (GridViewRow row in GridView1.Rows)
{
var checkbox = (CheckBox)row.FindControl("CheckBox1");
if (checkbox.Checked)
{
checkedItems.Add(int.Parse(row.Cells[1].Text));
}
}
Session["checkedItems"] = checkedItems;
Response.Redirect("Page2.aspx");
}
Page 2
protected void Page_Load(object sender, EventArgs e)
{
var checkedItems = (List<int>)Session["checkedItems"];
Session["checkedItems"] = null;
foreach (var checkedItem in checkedItems)
{
Response.Write(checkedItem);
}
}
Using the IDs in the checkedItems List you can now query those from you DB and finally assign the Result to your GridView on the second page.
Instead of using Session you could pass the IDs via QueryString.
Ive no problems using Javascript to read the rows of a telerik radgrid component im using however I can seem to find anyway to access the row data server side when a postback occurs. Ive spent ages looking for solution but no luck.
Any pointers would be greatly appreciated.
Tony
You might want to look at the DataKeyValues property of the OwnerTableView object, which will let you access a collection of values that represent the fields in a given row. I use it during the EditCommand event handler, since a user of my site is directed to an edit page if they click on the link to edit a row in the grid, and I need to pass along certain info about the given row in the query string.
If this turns out to be what you need, you'll also need to define which fields should be made available through this property. To do that, look at the MasterTableView.DataKeyNames property in the property sheet for the grid. You basically specify a comma-delimited list of field names.
The server-side is the easy part:
GridItemCollection gridRows = TestGrid.Items;
foreach (GridDataItem data in gridRows)
{
ItemClass obj = (ItemClass)data.DataItem;
}
It's the client side part that I don't know! :[
private Int32 GetID()
{
foreach (Telerik.Web.UI.GridDataItem dataItem in radGrid.MasterTableView.Items)
{
if (dataItem.Selected == true)
{
Int32 ID = (Int32)dataItem.GetDataKeyValue("ID");
return ID;
}
}
throw new ArgumentNullException("Id Not found");
}
This is the one that works for me and uses the RadGrid.SelectedItems collection.
protected void LinkButton1_Click(object sender, EventArgs e)
{
List<Guid> OrderIdList = new List<Guid>();
foreach (GridDataItem OrderItem in this.RadGrid1.SelectedItems)
{
OrderIdList.Add(new Guid(OrderItem.GetDataKeyValue("OrderId").ToString()));
}
}
If you correctly created your controls in markup or page init for dynamic controls, then the RadGrid will properly restore state.
You can access the initial values that were loaded from the data source like this example below, provided you told the table view in question to keep the columns around in the data keys.
protected T GetInitialGridData<T>(GridDataItem item, string uniqueColumnName) {
item.ThrowIfNull("item");
uniqueColumnName.ThrowIfNullOrEmpty("uniqueColumnName");
return (T)item.OwnerTableView.DataKeyValues(gridItem.ItemIndex)(columnName);
}
If you are using a dynamic custom template column, and need to get to any values that may now be in their states, you can use:
protected string GetCustomTextBoxValue(GridDataItem item, string controlID) {
item.ThrowIfNull("item");
controlID.ThrowIfNullOrTrimmedEmpty("controlID");
return ((TextBox)item.FindControl(controlID)).Text;
}
private Int32 GetID()
{
foreach (Telerik.Web.UI.GridDataItem dataItem in radGrid.MasterTableView.Items)
{
if (dataItem.Selected == true)
{
// Int32 ID = (Int32)dataItem.GetDataKeyValue("ID");
Int32 ID =Convert.ToInt32(dataItem.GetDataKeyValue("ID"));
return ID;
}
}
}
//this will work