Get data into gridview devexpress - devexpress

In gridcontrol devexpress, I display "employee_id" column as combobox. I want to fill data from EMPLOYEES table into "employee_id" column in gridcontrol devexpress. Thanks.

I suggest that you handle the GridView.ShownEditor event. Within this event handler, you can obtain the currently displayed clone of the column editor, and filter its items. Here is some sample code:
private void gridView_ShownEditor(object sender, EventArgs e) {
GridView view = (GridView)sender;
if (view.FocusedColumn != employee_id) return;
ComboBoxEdit editor = (ComboBoxEdit)view.ActiveEditor;
// Here bind the editor DataSource and ValueMember and
Display member to employee_id
}
Other way as:
Ref: How to bind dataset with different values to repository combobox in runtime
RepositoryItemComboBox combo = new RepositoryItemComboBox();
combo.Items.AddRange(new string[{values});
gridControl1.RepositoryItems.Add(combo);
gridView1.Columns["Criteria"].ColumnEdit = combo;
Ref:
How to populate the RepositoryItemCheckedComboBox with item based on the selection made in another cell
combobox in gridcontrol
Column Value List

Related

After editing detail record, wrong record is selected in gridview

I have a GridView displaying several records. Clicking on say the third record, displays a DetailsView below where I can edit the record. If I make changes to the record and save, the GridView refreshes and resorts and now my edited record is the 5th row. However, the 3rd row is still selected and the DetailsView now displays a different record than the one I just edited.
After an update via DetailsView, how do I make sure THAT record is reselected in the GridView no matter where it may now be in the GridView sort order?
You can keep track of the ID of each row by adding the field to the DataKeyNames of the GridView. Before you refresh the grid in code-behind, you remember the ID of the selected row (assuming here that the relevant ID is a field called ClientID):
int savedClientID = (int)gvClients.DataKeys[gvClients.SelectedIndex].Values["ClientID"];
You can then look for it after binding the data and select the corresponding row:
void gvClients_DataBound(object sender, EventArgs e)
{
gvClients.SelectedIndex = -1;
foreach (GridViewRow row in gvClients.Rows)
{
int clientID = (int)gvClients.DataKeys[row.RowIndex].Values["ClientID"];
if (clientID == savedClientID)
{
gvClients.SelectedIndex = row.RowIndex;
break;
}
}
}
UPDATE
If paging is turned on in the GridView, you may have to use the method suggested in this post: GridView in ASP.Net -- Selecting the correct row

How to display the master-detail tables into two xtragrid?

I have to tables:users and messages.And create the one-many relations between these tables.And,If I display this two table in single xtragrid, it's no problem,But ,I want to display into two grid,the detail of messages doesn't display. please help me!
the key code is :
1.create the dataset of users and messages:
DataSet ds = new DataSet();
ds = SqlHelper.ExecuteDataset(fbh.ConnectionString, CommandType.Text, s);
DataTable mess = ds.Tables[0];
mess.TableName = "Messages";
DataTable user = deptmentUserMessages.Tables["Users"];
DataTable m = new DataTable("Messages");
m.Merge(mess);
deptmentUserMessages.Tables.Add(m);
deptmentUserMessages.Relations.Add("**UserMessages**", user.Columns["UserName"], m.Columns["SENDER"]);
the deptmentUserMessages is the static dataset
2.in the form.load event,I have to do:
gcMessage.DataSource = master;
gcMessageDetail.DataSource = detail;
master.DataSource = pub.DeptmentUserMessages;
master.DataMember = "Users";
detail.DataSource = master;
detail.DataMember = "**UserMessages**";
the master and detail are BindingSource.
You should handle xtargrid FocusedRowChanged event to populate detail grid.
Get the primary key field value to fetch values from child table and then set the data source of child grid. Use GetRowCellValue(Int32,String) Method
private void gridView1_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e) {
if(e.FocusedRowHandle >=0)
{
/// Get master table selected row's primary column value
/// then create a DataView from the detail table
// and set datasource of detail grid.
dvUserMessages = (Filtered row by primary column value);
master.DataSource = dvUserMessages.ToTable();
}
}
Reference:Parent - Child relationship between Two GridViews
Using only event FocusedRowChanged is not enough. When the DataSource is changed, this event doesn't fires. Because the position property doesn't changes, it remains 0. And it doesn't fires when user filters rows in master grid. So, add to this example similar event handler on ColumnFilterChanged event, and also populate child grid just after setting DataSource property of master grid.

Gridview cell value and array list

When I am tryng to pass the gridview cell values to an array everything is perfect but the array is empty after executing the code.
When I see through debug mode the index of selected row also is fine, i.e. when I select two rows using checkbox among 1000 the count shows exactly 2 in the array but the data is empty.
I'm not able to get the cell value from the gridview.
Any advice would be greatly appreciated.
protected void Button2008_Click(object sender, EventArgs e)
{
ArrayList checkedItems = new ArrayList();
foreach (GridViewRow row in this.GridView1.Rows)
{
if (((CheckBox)row.FindControl("cbRows")).Checked == true)
{
checkedItems.Add(row.Cells[12].Text);
}
}
Session["CheckedItems"] = checkedItems;
Response.Redirect("About.aspx", true);
}
you can use a breakpoint and check where the problem is occuring, for instance, does row.Cells[12].Text show any value ?
and you can check how your aspx page is acting after the postback.

How can I retrieve a subset of data from entity object data source and pass to another page

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.

set label text to total row count of gridview

I'm using a stored procedure in a sql database as the data source for a SqlDataSourceControl on my .aspx page. I'm then using the SqlDataSourceControl as the data source for a gridview on my page. Paging is set to true on the gridview. What i would like to do is set the text of a label to the total number of rows in the gridview. I can use this code
'labelRowCount.Text = GridView2.Rows.Count & " layers found"
to return the number of results per page, but it doesn't give me the total. I've looked in several places and haven't been successful in finding a solution.
You should use the underlying datasource that the gridview is bound to (grid.DataSource). For instance if you have bound the grid to a datatable then just cast the grids datasource into the datatable and the use the rows.count property to get the total record count. Another alternative would be to get a reference to the the grids datasource object before you set it to the grid so you can get the record count directly.
So for example (assuming you are bound to a DataTable)
int count = ((DataTable)grid.DataSource).Rows.Count;
Enjoy!
Put an event handler on "selected" for the SQL DataSource. That event handler has an argument of type SqlDataSourceStatusEventArgs. In there AffectedRows is the row count of the entire data set (not just that shown on the current page). So catch that and write it out to your label:
protected void SqlDataSource_Selected(object sender,SqlDataSourceStatusEventArgs e)
{
if (e.Exception != null)
{
// do something useful, then...
e.ExceptionHandled = true;
}
else
labelRowCount.Text = String.Format("{0} layers found", e.AffectedRows);
}
GridView2.Rows saves only the rows that are visible, so when page-size is 5 you get only 5 records. As Doug suggested you have to set the labelRowCount.Text ondatabound and not on every postback, because on postback - when the datasource is not binded again - the datasource will be nothing. So a good place could be where you bind the grid to the datasource.

Resources