LINQ to Entity Framework, Multiple Joins…

Here is a little example to doing multple joins using LINQ with EF:

var query = context.EquipmentBases

  .Where(e => e.Model == model && e.Serial == serial && e.Suffix == suffix)

  .Join(context.TruckBases, e=>e.EquipmentBaseID, t =>t.EquipmentBaseFK, (e,t)=>new{e,t})

  .Join(context.TruckSupplemental_Hitch, et=>et.t.TruckID, h=>h.TruckFK, (et,h)=>new{et,h})

  .Join(context.TrailerPlugTypeLTs, eth=>eth.et.t.TrailerPlugTypeFK, p=>p.ID, (eth, p)=>new{eth,p})

  .Select(r => new { r.eth.et.e.CurbWeight, r.eth.et.e.MaxLoad, r.eth.h.BallRating, r.eth.h.ReceiverRating, r.eth.h.BallSize, r.eth.et.e.GVWR, r.eth.h.BallHeight, r.eth.et.t.IntegratedHitchSystem, r.p.TrailerPlugType });

Note the pattern in the joins. This is what makes it all work.

Set width of GridView edit field textbox…

How can the width of an edit text box be set in a GridView control where EnableSortingAndPagingCallback is true? Microsoft’s documentation for GridView.EnableSortingandPagingCallbacks states "If the Columns collection contains a column that does not support callbacks, such as TemplateField, a NotSupportedException exception is raised." So this rules out the use of a template column. I tried the template column and found it caused an error as advertised. I posted this question on Express Exchange but received only one bogus response.

Here is my GridView code…

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%"
            AllowSorting="True" AllowPaging="true" PageSize="12"   
            PagerSettings-Mode="Numeric"
            OnRowDataBound="BlackberryView_OnRowDataBound"
            EnableSortingAndPagingCallbacks="True" EmptyDataText="No records found!"
            DataSourceID="SqlDataSource1" AutoGenerateEditButton="True"
            OnRowUpdating="BlackberryView_OnRowUpdating"  EditRowStyle-Width="100%">
            <HeaderStyle CssClass="blackberryviewheader" ForeColor="White" Wrap="False" />
            <RowStyle CssClass="blackberryviewrow" ForeColor="#003366"  />
            <AlternatingRowStyle CssClass="blackberryviewalternatinfrow" />
            <EditRowStyle BackColor="Red" Width="100%" />
            <PagerStyle ForeColor="#003366" Font-Bold="true" HorizontalAlign="Center" />
        </asp:GridView>

After much searching of the internet, and only one bogus response from "Experts Exchange" I set to work my to solve the problem myself. Here is my work around…

  • Create a integer variable, editRow, and set its value to -2
  • When the OnEditRowEditing event fires, set editRow to the value of the selected row index.
  • As the ViewGrid re-builds, check for the row selected for editing. When found, check the specific cell for an empty string. If the cell contains a text box control the cell Text value will be "".
  • Next cast the cell’s first control to a TextBox then you can set its various properties including it’s width.

Int32 editRow = -2;

protected void GridView1_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
        TextBox tb = null;
        if (e.Row.RowIndex == editRow)
        {
            sVal = e.Row.Cells[2].Text;
            if (e.Row.Cells[2].Text == "")
            {
                tb = (TextBox)e.Row.Cells[2].Controls[0];
                tb.Wrap = true;
                tb.Width = Unit.Pixel(740);
                tb.Rows = 4;
                tb.TextMode = TextBoxMode.MultiLine;
                tb.Height = Unit.Pixel(60);
            }
       }
}

protected void GridView1_OnRowEditing(object sender, GridViewEditEventArgs e)
{
    editRow = e.NewEditIndex;
}