How to Use the Select Command in the DataGrid in Asp.net

How to Use the Select Command in the DataGrid in Asp.net thumbnail
The DataGrid control displays data in an HTML table

The Asp.net DataGrid control transforms data sent from a database into a JavaScript-enhanced HTML table suitable for display on a web page. Programmers typically use the DataGrid to specify the appearance and behavior of their data. For advanced scenarios, programmers work with the "Select" command and associated properties such as SelectedItemStyle and SelectedItem, which Microsoft incorporated into the DataGrid to allow web pages to respond to user input. Microsoft deprecated the DataGrid and replaced it with the GridView. Most programmers use the DataGrid only in legacy applications.

Things You'll Need

  • Visual Studio
Show More

Instructions

    • 1

      Create a DataGrid instance by dragging and dropping a DataGrid onto your Asp.net page from Visual Studio's Toolbox.

    • 2

      Modify the DataGrid in code by typing the following, for example, in your code-behind file:

      MyDataGrid.ShowFooter= True

    • 3

      Attach an event handler in your ASP or ASPX code per the following example, which attaches the "MyEditEventHandler" to the DataGrid's edit event.

      <asp:DataGrid id="MyDataGrid" OnEditCommand="MyEditEventHandler"/>

    • 4
      Tell Asp.net what to do when a user clicks on the DataGrid's "Edit" button.
      Tell Asp.net what to do when a user clicks on the DataGrid's "Edit" button.

      Attach a method to the DataGrid's edit button. Access its click event to manually control the DataGrid's behavior. Press any of the DataGrid's auto-generated "edit" buttons on a live web page to trigger the event.

      'Tell the DataGrid to run the MyDataGrid_Edit function when the

      'MyDataGrid Edit Command is triggered

      AddHandler MyDataGrid.EditCommand, AddressOf MyDataGrid_Edit

    • 5

      Add a method to execute when the user triggers the "Select" command.

      'Create the MyDataGrid_Edit command

      Sub MyDataGrid_Edit(sender As Object, e As DataGridCommandEventArgs)

      'Code to execute when the user hits the edit button

      End Sub

    • 6

      Change the color of an item in your ASPX code by assigning a string to its Backcolor property. Retrieve and edit the style of the selected item in a DataGrid control to fit your design specification.

      <EditItemStyle BackColor="red"> </EditItemStyle>

Tips & Warnings

  • Apply these techniques to other properties and methods of the DataGrid control to change border colors and widths, to sort data and page data, and more.

  • The DataGrid displays non-HTML encoded text. Thoroughly scrub any user input you intend to display in the DataGrid.

Related Searches:

References

Resources

  • Photo Credit Jupiterimages/Photos.com/Getty Images Jupiterimages/Polka Dot/Getty Images

Comments

Related Ads

Featured