Entity Framework GridView RowDataBound

I found it tricky to get actual entities in the GridView RowDataBound event.

Per Diego Vega’s post here, this may only happen because I am using the EntitySetName property of the EntityDataSource.

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
            Dim descriptor As ComponentModel.ICustomTypeDescriptor = e.Row.DataItem

If descriptor IsNot Nothing Then
                Dim prop = descriptor.GetProperties().Cast(Of ComponentModel.PropertyDescriptor)().First()

Dim Order As Order = DirectCast(descriptor.GetPropertyOwner(prop), Order)

Response.Write(“Order Number:” & Order.OrderNumber)
End If
End If
End Sub

jQuery Get Next Textbox in a GridViewRow

In a GridView I have a column of checkboxes and a column of textboxes. I wanted to modify the value of the textbox when the checkbox is checked. jQuery is the perfect fit for this. Here is the code I used:

$(function () {
    var $inp = $('input:checkbox');
    $inp.bind('click', function () {
        var textbox = $(":input:text:eq(" + $inp.index(this) + ")");

        if (this.checked == true) {
           
textbox.val('123ABC');
            textbox.attr('disabled', 'disabled');
        } else {
            textbox.attr('disabled', '');
        }
    });
});

This post uses ideas from Suprotim Agarwal.

How to Make Favicons with Visual Studio

I don’t have any fancy program to generate an icon. Since I always forget how to do this, here are simple instructions:

Open the logo in Photoshop and flatten it to one layer on a transparent background. Duplicate it and resize to 16×16 pixels. Open a new instance of Visual Studio and click New File. Select Icon and it will open a pane with two versions. The first version is 16×16, and will appear in browsers. There is also a 32×32 version, which I always do just in case someone adds a link to their desktop.

Set up the image type in Visual Studio. The default image type is 4 bit, and you want 24 bit. Press Insert on your keyboard and select “16×16, 24 bit” then delete the 4 bit image type.

Go to Photoshop and copy the entire canvas to the clipboard. Return to Visual Studio and CTRL+A, Delete, CTRL+V. Switch to the 32px version and the thumbnail for the 16px version should update.

Back in Photoshop, close the 16px version and resize the original to 32px. Copy it into Visual Studio the same way as above. Save as favicon.ico, and upload it.

The HTML code I use is as follows:

<link rel="SHORTCUT ICON" type="image/x-icon" href="/favicon.ico" />

GridView Row Index in Markup

Sometimes I want to show the e.Row.RowIndex of a GridViewRow in the markup of the page. I don’t want to hook into the RowDataBound method just to display a simple number. The answer varies for GridViews and Repeaters. Click here for the complete rundown.

GridView:

<asp:Literal runat="server" Text='<%# Container.DataItemIndex + 1 %>' />

Repeater:

<asp:Literal runat="server" Text='<%# Container.ItemIndex + 1 %>' />

.Net Options for Data Access

Data Access Practices Using Microsoft .Net: A Nerdly Comparison

In this article a comparison of these options:

– Connected Data Access with ADO.NET
– Disconnected Data Access with ADO.NET and Typed DataSets
– Basic Object Relation Mapping with LINQ to SQL
– Object Relational Mapping with LINQ to Entities and the Entity Framework

See also: Extending NerdDinner: Exploring Different Database Options