views:

62

answers:

1

Hello,

I have table in my database names User with fields:

Id FirstName LasteName LoginName

Now I would like to present this table in ASPxGridView using LinqServerModeDataSource.

What I did is :

<dxdtlnq:LinqServerModeDataSource ID="LinqServerModeDataSource1" runat="server"          OnSelecting="LinqServerModeDataSource1_OnSelecting"
        ContextTypeName="MyContext" EnableDelete="True" 
        EnableInsert="True" EnableUpdate="True" TableName="Users" >
    </dxdtlnq:LinqServerModeDataSource>

protected void LinqServerModeDataSource1_OnSelecting(object sender, LinqServerModeDataSourceSelectEventArgs e) {

MyContext context = new MyContext();

    var qry = from s in context.Users select s;
    e.QueryableSource = qry;
}

That works great with my ASPxGridView. I can display data, insert and delete but now I would like to have additional column UserName which is FisrtName + LastName.

So I did something like that:

Added appropriate column to my ASPxGridView (FieldName = "UserName") and modified OnSelecting handler:

protected void LinqServerModeDataSource1_OnSelecting(object sender, LinqServerModeDataSourceSelectEventArgs e) {
            KozuModelDataContext context = new MyContext();

        var qry = (from s in context.Substitutions
                   select new
                              {
                                  Id = s.Id,
                                  FirstName = s.FirstName,
                                  LastName = s.LastName,
                                  LoginName = s.LoginName,
                                  UserName = s.FirstName + " " + s.LastName,
                              }).AsQueryable();

        e.KeyExpression = "Id";
        e.QueryableSource = qry;
    }

Now data in the grid is displayed buyt when I want to insert or edit data fields cannot be filled, textboxes doesnt respond in inserting form I cant type in any text.

Is there any solution for inserting and editing data in this manner ?

Thanks for help

+1  A: 

Hi,

I have tried to reproduce this issue and see this behavior. To be able to edit data using this approach, I suggest that you do the following:

1) Handle the ASPxGridView.CellEditorIntitialize event and set the e.Editor.ReadOnly property to false. This will allow the end-user to modify data in the EditForm editors;

2) Handle the ASPxGridView.RowUpdating (RowInserting) event and update data manually. Also, you should set the e.Cancel parameter to true (to prevent the grid from updating data itself) and also call the GridView's CancelEdit method to close the EditForm.

I should also mention that there is no need to fetch data for the UserName from the DB. This can be done using the ASPxGridView's CustomColumnDisplayText event handler:

protected void ASPxGridView1_CustomColumnDisplayText(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDisplayTextEventArgs e) {
    if(e.Column.FieldName == "") {
        object[] values = ASPxGridView1.GetRowValues(e.VisibleRowIndex, new string[] { "FirstName", "LastName" }) as object[];
        e.DisplayText = values[0].ToString() + " " + values[1].ToString();
    }
}

If this approach works for you, you can avoid using the Selecting event and thus set the LinqServerModeDataSource's TableName. This will allow you to provide the data editing feature not using the approach I explained above.

DevExpress Team