:: Home     :: MS Dynamics CRM     :: .Net 1.1     :: .Net 2.0     :: Sharepoint Portal     :: Ajax

  login:        
  passwords:  
 

Asp.Net Interview Questions
Asp.Net 2.0/3.5 Sample Interview Questions and Answers. Interview Tips for a successful job Interview


 

Code Walkthroughs

Datagrid Formatting the Data
We are able to format the content of the datagrid cell by one of two simple methods, dependant upon whether the column is a bound column or whether it is a template column. In our example we shall format the column to have to digits after the decimal point , followed by a...

Datagrid Highlight a Row With Click Through
It is relatively easy to add alternating colours to the rows in your datagrid. However, when we move the mouse over the rows we may want to highlight this row, and possibly to add the option of a click through based on the row selected...

Add a Delete Button to a Datagrid
To add a delete button to a datagrid follows a similar process to adding an edit button. In the datagrid header...

Add an Edit Button to a Datagrid
The datagrid has a predefined editColumn for handling the editing of a datagrid. Adding this simple column definition to a datagrid adds a powerful feature. When a row is not in edit mode the column item shows the word...

Making a Datagrid Row Editable
Two of the most popular methods of editing a datagrid in asp.net are to either select the row and take the user off to a different presentation of the data, or to change the formatting of the row presented in the database with appropriate edit text boxes, checkboxes and...

Adding Tooltips to Datagrid Rows
Adding tooltips to datagrid rows is easy, assuming that you have already created the code for adding row highlighting. In this article I shall assume that you have already read the article entitled Datagrid Highlight a Row With Click Through...

Binding a Datagrid to an Access Database
This list covers the full lifecycle of a content management system, from initially creating the content, through to delivering it to end users...

Adding Data to a DropDownList
The aim of this article is to answer the question 'How do I add items to a DropDownList?' Initially as part of the declaration for the DropDownList we can also define a number of items, much in the same way as in classic ASP...

Getting Current Date Time
In classic ASP we had now() which would return the current date and time. For asp.net this no longer exists. So what should we use...

Test if File Exists
Sometimes, in order to reduce our chance of error, when working with the filesystem in ASP.NET, we need to determine wether a file exists before performing an action on it. The following short piece of code will enable us to test whether a file exists...

Using Javascript with ASP.NET Form Elements
Adding simple pieces of Javascript to an Asp.net page can be acheived by adding to the attributes of the particular imagebutton or linkbutton. if its normal ASP.Net Button then you can...

Regular Expressions
In the table below we list the characters used in .Net regular expressions, together with their meaning, But first...

Authentication in Asp.net
Forms authentication in ASP.Net is far more easier and safe than Asp 3. It is possible to place a web.config file in any directory of a web site.Therefore, we are able to make most of a web site public, whilst providing authentication on, say, one directory...

Discussion Forums

General ASP.NET

.Net Programming

cSharp Home

Sql Server Home

Javascript / Client Side Development

IT Jobs

Ajax Programming

Ruby on Rails Development

Perl Programming

C Programming Language

C++ Programming

Python Programming Language

Laptop Suggestions?

TCL Scripting

Fortran Programming

Scheme Programming

25. DataGrid

FAQ Home
   25.1 Why do I get the Columns twice in the datagrid. I am using BoundColumns and TemplateColumns in DataGrid?
   25.2 How to Format and display currency with n decimal points in a BoundColumn.
   25.3 How do I specify more than one parameter for my HyperlinkColumn?
   25.4 Why am I getting an 'AllowCustomPaging must be true and VirtualItemCount must be set for a DataGrid with ID MyDataGrid when AllowPaging is set to true and the selected datasource does not implement ICollection' Error?
   25.5 How do I use a "helper function" to change the column value as Boolean in database to be displayed as Yes/No in Datagrid?
   25.6 How to display only date part in the Datagrid if the Date is of DateTime datatype in the database?
   25.7 How to open a new window with multiple parameters when clicked on a hyperlink in a column in a datagrid?
   25.8 How to display hierarchical data in a DataGrid?
   25.9 How do I conditionally set the text color of a cell in my Datagrid based on the cell's/fields's value?
   25.10 When I open my new window using Javascript, all elements appear great, but the original window just displays [Object] why?
   25.11 How to populate a DataGrid using a DataReader?
   25.12 How to populate a DataGrid using a DataSet?
   25.13 Why do I get "Could not lock file" and "cannot open file" exceptions when bound to a mdb file?
   25.14 I am binding the DataGrid to a datasource at runtime. After binding I want to populate a listbox with the column headers from the datagrid. How can I reference the column headertext for each column in the datagrid and then add them to my listbox?
   25.15 When I try to do an Update from my Datagrid, I keep getting the old/original values. Why?
   25.16 How can I sort a BoundColumn?
   25.17 How can I hide the HeaderText of the DataGrid?
   25.18 How to add a mailto link inside a datagrid?
   25.19 How to do Simple sorting using DataGrid?
   25.20 How to do Paging using DataGrid?
   25.21 How to display an image field in each row of DataGrid?
   25.22 How to get the DataField name of a BoundColumn from code-behind?
   25.23 How to confirm delete in DataGrid using PushButton?
   25.24 How to do Bidirectional Sorting in the DataGrid?
   25.25 How to have mutually exclusive radiobutton controls in a datagrid.
   25.26 How to add a TemplateColumn dynamically to the datagrid?
   25.27 How to edit data in the DataGrid using a TemplateColumn?
   25.28 How do I use Validator controls while editing data in the DataGrid?
   25.29 How to highlight the Column that is sorted in a DataGrid?
   25.30 How to create a Drilldown DataGrid?
   25.31 How to set a Datagrid Column to invisible when that Column is an AutoGeneratedColumn?
   25.32 How to use the DataFormatString to format DataGrid data dynamically?
   25.33 How to set the maxlength of a textbox (in the EditTemplate of DataGrid) based on a field value in the record?
   25.34 How to add an attribute to the Table generated by the DataGrid?
   25.35 Why do I get the error message " CS1502: The best overloaded method match for 'xxx(string)' has some invalid arguments " when I use Helper function in DataGrid?
   25.36 How to confirm delete in DataGrid using LinkButton?
   25.37 How to hide and show a Column based on the authenticated user?
   25.38 How to use a dropdownlist in a DataGrid?
   25.39 How to set a different color for every alternate row of a datagrid?
   25.40 How to add a counter column to a DataGrid?
   25.41 How to change the HeaderText of the Datagrid?
   25.42 How to select a record in the datagrid and start editing/updating the record using textboxes?
   25.43 How can I show what page the user is on using in-built paging functionality. CurrentPageIndex Property Shows Up as 0?
   25.44 How to display "No data" when a field value is null?
   25.45 How to change the value of a field before it gets displayed in the datagrid?
   25.46 How to use a ButtonColumn in a DataGrid?
   25.47 How to display a Master Detail data using DataGrid?
   25.48 I have set the ItemStyle and the AlternatingItemStyle tags for DataGrid control but they aren't working correctly , why?
   25.49 How to display the checkbox checked or unchecked for a bit data type value in the DataBase?
   25.50 How to do alphabetical paging in ASP.NET?
   25.51 How to enable or disable the button in the DataGrid based on a bit value in the database?
   25.52 How to show data vertically in a datagrid for a particular row?
   25.53 How can I fix error message "Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount"?
   25.54 How to edit a record using DataGrid?
   25.55 How to delete a record using DataGrid?
   25.56 How to use a HyperlinkColumn for a DataGrid?
   25.57 How to use a LinkButton in a DataGrid?
   25.58 How can I display the field as a link in the DataGrid? If may or may not be stored in the "http://" format.
   25.59 How to hide a column in a Datagrid if AutoGenerateColumns is set to True?
   25.60 How do I change the width of the Textboxes created for edit-mode of a Datagrid?
   25.61 How to display a Tooltip when hovering over the Header sort link of the DataGrid?
   25.62 How to right align cells in the Datagrid when using BoundColumn?
   25.63 How to hide a row in a DataGrid if one of the Column value is zero?
   25.64 How can I disable sorting for a specific Column in a DataGrid?
   25.65 Why do I get error message "NewPageIndex is not a member of "System.EventArgs"."?
   25.66 How to create a Main Header in DataGrid along with the DataGrid Column Headers?
   25.67 Is there a way to show more than one datafield in a column when using a datagrid?
   25.68 How to databind a textbox in a column template that is inside a datagrid?
   25.69 How can I simulate a scrollbar in a DataGrid?
   25.70 How to clear a DataGrid?
   25.71 How to trigger the checkChanged event of the checkbox inside a DataGrid?
   25.72 How to display "No data exists" within the datagrid rather than just showing Column Headers with no rows?
   25.73 How to fill a DataGrid with an array?
   25.74 How to add a Column dynamically to the datagrid to include custom expressions?
   25.75 How to access the BoundColumn information on SelectedIndexChanged event of a datagrid?
   25.76 How to access the TemplateColumn information on SelectedIndexChanged event of a datagrid?
   25.77 How to access information from the controls in the TemplateColumn in SelectedIndexChanged Event?
   25.78 How to display the total of a particular column at the footer of the DataGrid?
   25.79 Why does the DataGrid web server control contents wrap when ItemStyle Wrap or HeaderStyle Wrap Property is set to False?
   25.80 How can I have an onclick event in the DataGrid for any Column?
   25.81 How should I avoid getting blank page on click of linkbutton inspite of setting the EnableViewState= true for DataGrid?
   25.82 Why do I get a blank page when I click the linkbutton in the Datagrid, I am also handling PostBack on the page? The ItemCommand Event does not seem to trigger?
   25.83 How to export data in Datagrid on a webform to Microsoft Excel?
   25.84 How to export DataGrid data to excel?

25.1 Why do I get the Columns twice in the datagrid. I am using BoundColumns and TemplateColumns in DataGrid?


Set the AutogenerateColumns= False. By Default it is set to true for a datagrid


25.2 How to Format and display currency with n decimal points in a BoundColumn.


Set the Dataformatstring of the boundcolumn to {0:Nn} where n=> number of decimal points For more details refer Format Function


25.3 How do I specify more than one parameter for my HyperlinkColumn?



<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn HeaderText="Sample Column">
<ItemTemplate>
<asp:Hyperlink runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "ProductName").ToString()%>' NavigateUrl='<%# "page1.aspx?id=" + DataBinder.Eval(Container.DataItem,"ProductId").ToString() + "&Name=" + DataBinder.Eval(Container.DataItem,"ProductName").ToString()%>' ID="Hyperlink1" NAME="Hyperlink1"/>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>



25.4 Why am I getting an 'AllowCustomPaging must be true and VirtualItemCount must be set for a DataGrid with ID MyDataGrid when AllowPaging is set to true and the selected datasource does not implement ICollection' Error?


You are probably trying to implement paging in a DataGrid while binding the DataGrid with a DataReader. To fix this, instead of using a DataReader use a DataSet


25.5 How do I use a "helper function" to change the column value as Boolean in database to be displayed as Yes/No in Datagrid?



<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
<Columns>
     <asp:TemplateColumn HeaderText="Status">
     <ItemTemplate>
          <%#ShowStatus(bool.Parse(DataBinder.Eval(Container.DataItem,"Discontinued").ToString()))%>
     </ItemTemplate>
     </asp:TemplateColumn>
</Columns>
</asp:DataGrid>


VB.NET


protected Function ShowStatus(ByVal someval as boolean)
If someval = true Then
ShowStatus = "Yes"
Else
ShowStatus = "No"
End If
End Function


C#


protected string ShowStatus(bool someval)
{
     if (someval == true)
     {
     return "Yes";
     }
     else
     {
     return "No";
     }
}



25.6 How to display only date part in the Datagrid if the Date is of DateTime datatype in the database?


Set the DateFormatString as {0:d}


25.7 How to open a new window with multiple parameters when clicked on a hyperlink in a column in a datagrid?


The column should be defined as a TemplateColumn as follows and the NavigateUrl for that hyperlink can be set as follows to open a new window with parameters varying based on the row in which the hyperlink is present.


<asp:TemplateColumn >
<ItemTemplate>
<asp:Hyperlink ID="Hyperlink2" Runat="Server"
NavigateUrl= <%#"javascript:my_window=window.open('webform2.aspx?id=" + DataBinder.Eval(Container.DataItem,"productid").ToString() + "&Supplierid= "+DataBinder.Eval(Container.DataItem,"SupplierID").ToString()+"','my_window','width=300,height=300');my_window.focus()" %>
text=<%#DataBinder.Eval(Container.DataItem,"ProductName").ToString() %>>
</asp:Hyperlink>
</ItemTemplate>
</asp:TemplateColumn>


The above approach would avoid the problem of showing the screen of [Object] on the parent page


25.8 How to display hierarchical data in a DataGrid?


Check out Denis Bauer's custom control HierarGrid


25.9 How do I conditionally set the text color of a cell in my Datagrid based on the cell's/fields's value?


In the ItemDataBound event you can access the Cells() collection of the current Datagrid item (or row) and set it's ForeColor.


<asp:DataGrid OnItemDataBound="ItemDB" id="DataGrid1" runat="server"></asp:DataGrid>


VB.NET


protected Sub ItemDB(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
If (e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem) Then
     If e.Item.DataItem(6) > 15 Then
          e.Item.Cells(6).ForeColor = System.Drawing.Color.Green
     Else
          e.Item.Cells(6).ForeColor = System.Drawing.Color.Red
     End If
End If
End Sub


C#


protected void ItemDB(object      sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
     if((e.Item.ItemType ==ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem) )
     {
     if (Convert.ToInt16 (e.Item.Cells[6].Text) >15 )
          {
          e.Item.Cells[6].ForeColor = System.Drawing.Color.Green;
          }
     else
          {
          e.Item.Cells[6].ForeColor = System.Drawing.Color.Red;
          }
     }
}


In Cells[x]/Cells(x) x=> index number to the list of fields in the row.


25.10 When I open my new window using Javascript, all elements appear great, but the original window just displays [Object] why?


Check out How to open a new window with multiple parameters using datagrid?

 
Copyright 2005-2015, Megasolutions Ltd  |   Privacy Policy