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

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.

Related

How to join multiple table's data in one GridView in ASP.Net Webform on button_click

I am populating all Transaction data on one button click in a GridView. However, Transaction table in the database does not contain Dimension table (e.g. Account table) data such as Transaction table contains AccountID but it does not have AccountName property which I have to bring from the Account table. In the DataAccess layer the method (GetAllTransactions()) does contain the join between Transaction and Account table on AccountID, but when I try to select account.AccountName it does not work.
I just need to show few tables from Transaction table where the AccountID mataches and show the AccountName column instead of AccountID from the Transaction table.
The DataAccess method looks like:
public static IEnumerable<Transaction>GetAllTransactions()
{
List<Transaction> allTransactions = new List<Transaction>();
using (var context = new CostReportEntities())
{
allTransactions = (from t in context.Transactions
join account in context.Accounts on t.AccountID equals account.AccountID
select t).ToList();
}
return allTransactions;
}
and the Code behind the .aspx page is as follows:
protected void _UIButtonSubmit_Click(object sender, EventArgs e)
{
IEnumerable<Transaction> transactions = ts.GetAllTransactions();
_UITransactionGridView.DataSource = transactions;
_UITransactionGridView.DataBind();
_UITransactionGridView.PageIndex = 0;
}
tried few things with the following also, but I am not getting the LINQ idea. The following gives error.
...select new {t.*, account.AccountName}).ToList();
I need a direction which way should I be looking at to make the task work to populate data from multiple tables in one GridView. Thanks.
Since my Model for Transaction did not have any property of Account table I had to create a ViewModel type class which returns custom data of AccountNumber for every Transaction.
Tha code that I used to solve the problem is this

Get data into gridview 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

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.

ListView -Dynamic Controls and DataPager events

I have a listView control which is bound to an object datasource. In the ListView1_ItemDataBound event, I am generating some dynamic controls. I do this because depending on a particular column value, i might need to generate textbox, radio button, check box etc.
Some code here:
ListViewDataItem item = (ListViewDataItem)e.Item;
typez = Convert.ToInt32(DataBinder.Eval(item.DataItem,"Type").ToString());
if (typez == 1) //1 means generate radibutton
{
string[] options = DataBinder.Eval(item.DataItem, "QuestionDetail").ToString().Split(new string[] { delimiter }, StringSplitOptions.None);
questionID = Convert.ToInt32(DataBinder.Eval(item.DataItem, "Question_ID").ToString());
int optionCount = 1;
RadioButtonList rbl = new RadioButtonList();
//set ID for the radiobtnList to the questionid no.
rbl.ID = "mcq_" + questionID;
foreach (string s in options)
{
//adds the MCQ options to list item
ListItem li = new ListItem(Util.GetAlphabet(optionCount).ToUpper() + ". " + s, Util.GetAlphabet(optionCount).ToUpper(), true);
rbl.Items.Add(li);
optionCount++;
}
//PlaceHolder PlaceHolder1 = (PlaceHolder)e.Item.FindControl("PlaceHolder1");
PlaceHolder1.Controls.Add(rbl);
I have DataPager also. When I go to the next page I want to somehow capture the user response in the previous page. Otherwise, all the user response is lost. I tried using ListView1_PagePropertiesChanging event. But here I somehow do not seem to get the dynamic controls in the page.
I need to get the radio button selected value so that I can save it or put in some session variable so that when user comes back to this page he can see his previuos values.
Could someone please suggest some insight on what I am doing wrong.
Problem is that you have to create controls in OnInit, then ViewState persister can do it's job. If you do it after OnInit, in ItemDataBound, when Postback is triggered, controls doesn't exists and their's viewstate isn't deserialized.
To access values of radio buttons and get selected one, you have to look at Request.Form dictionary and find it.

Adding Rows to Temporary GridView

I want to have an ASP.net page where the user can add rows to a grid view (think typing values into a textbox and clicking Add to add to the grid view). Clicking a submit button would then persist all rows to the database.
For a low traffic site, what reasonably easy solution would you recommend to achieve this?
I've done this a few times. The basic premise of my solution is that you load the data into a local collection, and store this in the ViewState of the page.
List<MyObject> lst = new List<MyObject>();
// Populate the list from the database here
// Store this list in the ViewState
ViewState["List"] = lst;
I then have a function which binds this list to the GridView, which I call in the first Page_Load, and any function which modifies this list:
function BindList() {
List<MyObject> lst = (List<MyObject>) ViewState["List"];
GridView1.DataSource = lst;
GridView1.DataBind();
}
To add a new item...
function cmdAdd_Click(object sender, EventArgs e) {
// Retrieve list from ViewState
List<MyObject> lst = (List<MyObject>) ViewState["List"];
// Add the new item
MyObject newObj = new MyObject(); // Populate this from your form
lst.Add(newObj);
// Update the list in the ViewState
ViewState["List"] = lst;
// Update the grid to show the new item
BindList();
}
When you want to persist all of the items to the database, simply retrieve the list from the ViewState.

Resources