Asp.Net DataGrid View Interview Questions and Answers - 2

Question: 6

How do you apply specific formatting to the data inside the cells?

You cannot specify formatting for columns generated when the grid’s AutoGenerateColumns property is set to true, only for bound or template columns.

To format, set the column’s DataFormatString property to a string-formatting expression suitable for the data type of the data you are formatting.

Question: 7

How do you display an editable drop-down list?

Displaying a drop-down list requires a template column in the grid. Typically, the ItemTemplate contains a control such as a data-bound Label control to show the current value of a field in the record. You then add a drop-down list to the EditItemTemplate.

In Visual Studio, you can add a template column in the Property builder for the grid, and then use standard template editing to remove the default TextBox control from the EditItemTemplate and drag a DropDownList control into it instead. Alternatively, you can add the template column in HTML view.

After you have created the template column with the drop-down list in it, there are two tasks.

The first is to populate the list. The second is to preselect the appropriate item in the list — for example, if a book’s genre is set to “fiction,” when the drop-down list displays, you often want “fiction” to be pre-selected.

Related Questions