Telerik getting selected ID (Get data from Radgrid selected item) - c#

I can get the selected index of the gridview but i want to get the actual data that is inside the grid. I want to select a row in the grid and be able to access the "Client Id" column's actual data value. The grid is working fine and I am able to access the SelectedIndexChanged event. I have then been trying with no luck to find a way to get the information that is displayed in the grid. Any help would be greatly appreciated.
Again, I need to get access to all data that is displayed in the grid form the codebehind.

This is what data keys are for. Just designate the columns you want to access as data keys, like in the example shown below.
<telerik:RadGrid ID="RadGrid1" runat="server" ...>
<MasterTableView DataKeyNames="Column1, Column2, Column3" ...>
...
</MasterTableView>
</telerik>
Once the data keys have been assigned in the markup, you can access them in code-behind by row, or using the SelectedValues property.
if (RadGrid1.SelectedItems.Count > 0)
{
//access a string value
string column1 = RadGrid1.SelectedValues["Column1"].ToString();
//access an integer value
int column2 = (int)RadGrid1.SelectedValues["Column2"];
}

You could do it like this:
foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
{
if (item.selected == true)
string mydata = item["ColumnName"].Text;
}
I recommend you read the documentation on this site http://www.telerik.com/help/aspnet/grid/grdaccessingcellsandrows.html; it will sure help you a lot with Telerik components.

Use DataKeys as James Johnson suggested. You cannot access DataItem property of the GridDataItem in SelectedIndexChanged event. It will be null. According to Telerik documentation "DataItem object is available only when grid binds to data."
When DateItem is available, as in the ItemCreated event, you could do a cast to your original data type MyType:
private void RadGrid_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if ((e.Item is GridDataItem)) {
GridDataItem gridDataItem = (GridDataItem)e.Item;
MyType dataItem = (MyType)gridDataItem.DataItem;
}
}

Related

Sort a Grid when after loading all data in the grid

I have a grid with 4 columns 'Id' 'Name' ' Address' 'Age'
I getting all the Id's in the list and binding to the grid. Now in Grid_itemDataBound event depending upon the Id I am getting the Name, Address Age for that particular row.
now I want to sort the grid depending on the Name.
How I can Sort depending on the Name which was binded to grid in Item databound
Please do Needful
my Code
grdEmployee.DataSource = lstEmployee.OrderBy(x => x.Id).ToList();
protected void grdEmployee_ItemDataBound(object sender, EventArgs e)
{
Label lblname = (Label)e.Row.FindControl("lblName");
Label lblAge = (Label)e.Row.FindControl("lblAge");
Label lblId = (Label)e.Row.FindControl("lblId");
lblName.Text = GetName(lblId.Text);
lblAge.Text = GetAge(lblId.Text);
}
Note: this is sample code I have manually written in this editor. please Ignore faults in the code and please understand my scenario.
I think, it would be better if you get all the data at once, then doing it in parts in each call to ItemDataBound. This way instead of just a list of ID you will have a DataTable or a List of Employees. And then you can sort this DataTable or List the way you like - using DefaultView.Sort (for DataTable) or LINQ OrderBy.

How to get GridLookUpEdit's EditValue?

I have a GridLookUpEdit and wath to get a edit value on FocusedRowChanged event:
private void gridView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
{
var view = sender as GridView;
if (view != null)
{
MessageBox.Show(view.GetRowCellValue(e.FocusedRowHandle, view.Columns[1]).ToString());
}
}
But here i get a error says that i out of column array. But i have a two columns, first column visible, second not visible.
Whats wrong here? And how can i get EditValue more correctly if possible?
It seems that your view has only one column and there are two columns in your underlying datasource. So, you can get a value from underlying datasource.
If your underlying datasource is DataTable then you can use ColumnView.GetDataRow method:
MessageBox.Show(view.GetDataRow(e.FocusedRowHandle)[1].ToString());
If your underlying datasource is List<SomeObject> then you can use ColumnView.GetDataSourceRowIndex method:
MessageBox.Show(YourList[view.GetDataSourceRowIndex()].YourColumn.ToString());
Or you can add second column by using ColumnView.Columns collection:
var column = view.Columns.AddField("YourField");
column.Visible = false;

How to get field value of selected Row Devexpress GridView?

I use a DevexpressGridView to display all TOPIC (id,title,content)
<dx:ASPxGridView ID="gv" runat="server"
OnSelectionChanged="gv_SelectionChanged" >
I have grid_SelectionChanged event:
protected void gv_SelectionChanged(object sender, EventArgs e)
{
int id= selected row...???; //how can I get the value of selected row
string sql = "select * from TOPIC where idTOPIC="+id;
DataTable topic = l.EXECUTEQUERYSQL(sql);
TextBox1.Text = topic.Rows[0][1].ToString();
}
...
It seems gv.SelectedRow method isn't exist in DevGridview.
As recommended, I've tried with FocusedRowIndex method, but I really dont know the right syntax to get the value of selected row.
Help!!!
Changing the selection is different from changing the focused row. See the documentation for Selection for the difference between the two.
You can use gv.GetSelectedFieldValues to get the rows which are selected.
var ids = gv.GetSelectedFieldValues("id");
foreach( var id in ids )
DoSomethingWithObject(id);
You should handle the FocusedRowChanged event if you're interested in the focused row.
You can use the FocusedRowIndex value to index the rows of gv.DataSource, for example:
DataTable ds = (DataTable)gv.DataSource;
var id = ds.Rows[gv.FocusedRowIndex]["id"];
or you can use var id = gv.GetRowValues(gv.FocusedRowIndex, "id").
I've found my answere here after a long time searching google:
http://www.devexpress.com/Support/Center/Question/Details/Q347704
Use the ASPxGridView.GetSelectedFieldValues method get selected row values on the server side.
You can also get selected data row as
int rowHandle = gridView1.FocusedRowHandle;
if (rowHandle != DevExpress.XtraGrid.GridControl.InvalidRowHandle)
{
return this.gridView1.GetDataRow(rowHandle);
}
This would return DataRow
Please note this is when I am using Devexpress gridControl in WinForms
If you want to get only ID field value you can use this
int id = Convert.ToInt32(gv.GetRowValues(gv.FocusedRowIndex, "ID").ToString());
if you have an object you can use this
Personels selectedPersonel = gv.GetRow(gv.FocusedRowIndex) as Personels;
and get value method is
int ID = selectedPersonel.ID;

Using DataGrid ItemDataBound Event to Check Value

I've got a DataGrid where the datasource is bound with a SqlDataReader object:
SqlDataReader areader = runner.Reader;
dgSearchResults.DataSource = areader;
dgSearchResults.DataBind();
I've created an ItemDataBound event for my grid and I want to check the Value of a specific Column/Cell that while each item is bound so I can enable/disable some flags.
How can I get the value of a specific cell "SvcID" on the ItemDataBound?
Here is my code:
public void dgSearchResults_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (ViewState["SvcIDs"] != null)
{
if (e.Item != null)
{
var svcIds = (List<int>) ViewState["SvcIDs"];
if (svcIds.Contains(Convert.ToInt32(**DataGrid SvcID Goes Here**))
{
//TODO: enable/disable some icons
}
}
}
}
I've used RowDataBound events before but not for a DataGrid control, and the required steps are a bit different it seems.
What is the code to check the value of Column "SvcID" in my DataGrid against my SvcIds (using an Index) List?
Thanks
You can use the following to access the cells:
e.Item.Cells[index].Text;
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagriditemeventargs.item.aspx
if your text is in second cell.
ex
e.Item.Cells[2].Text
Unfortunately you cant index the Cell collection using a column name or the databound field name.
To do that you need to know the index of the datafield (e.g. SvcID in your case) and index the Row object. If you are using AutogenerateColumns then the index would be based on the order of columns you are querying from the database.
Then you can handle the RowDataBound event, which has an event argument of type GridViewRowEventArgs that gives u access to the DataGridView row instance. then you can do something like this:
string val= e.Row.Cells(index).Text;

asp.net (C#): get value of a non-displayed field from a gridview selection

I have a GridView control bound to an ObjectDataSource which returns a list of objects that look like this:
public class X {
public string DisplayName {
get;
}
public string Guid {
get;
}
}
I don't want to show the Guid property in the GridView, but I need to retrieve it when an object is selected.
I could not find a "DataValueField" property, like for the ListBox control.
As a workaround, I tried to set Visible=false for the column bound to the Guid property and get the cell text from the Row object in the SelectedIndexChanged method:
protected void GridView1_SelectedIndexChanged(object sender,EventArgs e){
GridViewRow row = GridView1.SelectedRow;
string id = row.Cells[2].Text; // empty
}
But this does not work - apparently if the column is not visible its Text property is left empty.
How can I get the Guid of the selected object while showing only the DisplayName?
Not sure about how this works with ObjectDataSource, but with SqlDataSource we set keys on the rows of the GridView.
GridView1.DataKeyNames = new String[] {"Guid"};
Then, you can get the key by doing this:
string guid = GridView1.DataKeys[GridView1.SelectedRow.RowIndex];
Add "Guid" into the DataKeyNames property of the GridView. That will allow it to be available even though the column is hidden.
Can't you use row.DataItem to get the object itself?

Categories

Resources