• Home
  • Privacy Policy
  • Terms & Conditions
  • Contact
  • Advertise
Videos, Photos, Wallpapers, Free Download, Movies, Songs, Sports , Live TV, Entertainment
 
  • Home
  • Bollywood
  • Hollywood
  • Box Office
  • Beauty
  • Fashion
  • Celebrity
  • Business
    • Social Media
    • Money Online
    • Category
  • Entertainment
    • Bollywood
  • Video
    • Youtube
    • Video
  • Technology
    • Asp.net
    • C#
    • SQL
    • .Net
Showing posts with label Array. Show all posts
An array is a data structure that contains a number of variables of the same type. Arrays are declared with a type:

type[] arrayName;

The following examples create single-dimensional, multidimensional, and jagged arrays:

class TestArraysClass
{
static void Main()
{
// Declare a single-dimensional array
int[] array1 = new int[5];

// Declare and set array element values
int[] array2 = new int[] { 1, 3, 5, 7, 9 };

// Alternative syntax
int[] array3 = { 1, 2, 3, 4, 5, 6 };

// Declare a two dimensional array
int[,] multiDimensionalArray1 = new int[2, 3];

// Declare and set array element values
int[,] multiDimensionalArray2 = { { 1, 2, 3 }, { 4, 5, 6 } };

// Declare a jagged array
int[][] jaggedArray = new int[6][];

// Set the values of the first array in the jagged array structure
jaggedArray[0] = new int[4] { 1, 2, 3, 4 };
}
}

An array has the following properties:
  • An array can be Single-Dimensional, Multidimensional or Jagged.
  • The default value of numeric array elements are set to zero, and reference elements are set to null.
  • A jagged array is an array of arrays, and therefore its elements are reference types and are initialized to null.
  • Arrays are zero indexed: an array with n elements is indexed from 0 to n-1.
  • Array elements can be of any type, including an array type.
  • Array types are reference types derived from the abstract base type Array.
The DataGrid control is a multi-column, data-bound ASP.NET server control. Using the DataGrid control, you can display records from a database using a variety of formats. You can also edit, update, and delete records from the database using the DataGrid control. In this first part of the ASP.NET server controls series, I will show you how to use the DataGrid control to develop compelling Web applications.

A data bound list control that displays the items from data source in a table. The DataGrid control allows you to select, sort, and edit these items.

DataGrid control to display the fields of a data source as columns in a table. Each row in the DataGrid control represents a record in the data source. The DataGrid control supports selection, editing, deleting, paging, and sorting.

Different column types determine the behavior of the columns in the control. The following table lists the different column types that can be used.

BoundColumn: Displays a column bound to a field in a data source. It displays each item in the field as text. This is the default column type of the DataGrid control.

ButtonColumn: Displays a command button for each item in the column. This allows you to create a column of custom button controls, such as Add or Remove buttons.

EditCommandColumn: Displays a column that contains editing commands for each item in the column.

HyperLinkColumn: Displays the contents of each item in the column as a hyperlink. The contents of the column can be bound to a field in a data source or static text.

TemplateColumn: Displays each item in the column following a specified template. This allows you to provide custom controls in the column.

By default, the AutoGenerateColumns property is set to true, which creates a BoundColumn object for each field in the data source. Each field is then rendered as a column in the DataGrid control in the order that each field appears in the data source.

You can also manually control which columns appear in the DataGrid control by setting the AutoGenerateColumns property to false and then listing the columns that you want to include between the opening and closing <Columns> tags. The columns specified are added to the Columns collection in the order listed. This allows you to programmatically control the columns in the DataGrid control.

The appearance of the DataGrid control may be customized by setting the style properties for the different parts of the control. The following table lists the different style properties.

AlternatingItemStyle: Specifies the style for alternating items in the DataGrid control.

EditItemStyle: Specifies the style for the item being edited in the DataGrid control.

FooterStyle: Specifies the style for the footer section in the DataGrid control.

HeaderStyle: Specifies the style for the header section in the DataGrid control.

ItemStyle: Specifies the style for the items in the DataGrid control.

PagerStyle: Specifies the style for the page selection section of the DataGrid control.

SelectedItemStyle: Specifies the style for the selected item in the DataGrid control.

The following table lists the properties that control which parts are shown or hidden.
ShowFooter: Shows or hides the footer section of the DataGrid control.
ShowHeader: Shows or hides the header section of the DataGrid control.

You can control the appearance of the DataGrid control by programmatically adding attributes to the <td> and <tr> tags rendered by the control on the browser. Attributes can be programmatically added by providing code in the event handler for the OnItemCreated or OnItemDataBound event.
To add an attribute to the <td> tag, first get the TableCell object that represents the cell in the DataGrid control you want to add the attribute to. The Control..::.Controls collection for the Item property of the DataGridItemEventArgs object passed into the event handler can be used to get the desired TableCell object. You can then use the AttributeCollection..::.Add method of the Attributes collection for the TableCell object to add attributes to the <td> tag.
To add an attribute to the <tr> tag, first get the DataGridItem object that represents the row in the DataGrid control you want to add the attribute to. The Item property of the DataGridItemEventArgs object passed into the event handler can be used to get the desired DataGridItem object. You can then use the AttributeCollection..::.Add method of the Attributes collection for the DataGridItem object to add attributes to the <tr> tag.
DataList is a data bound list control that displays items using certain templates defined at the design time. The content of the DataList control is manipulated by using templates sections such as AlternatingItemTemplate, EditItemTemplate, FooterTemplate, HeaderTemplate, ItemTemplate, SelectedItemTemplate and SeparatorTemplate. Each of these sections has its own characteristics to be defined but at a very minimum, the ItemTemplate needs to be defined to display the items in the DataList control. Other sections can be used to provide additional look and feel to the DataList control.



The contents of the DataList control can be manipulated by using templates. The following table lists the supported templates.
The DataList control displays data items in a repeating list, and optionally supports selecting and editing the items. The content and layout of list items in DataList is defined using templates. At a minimum, every DataList must define an ItemTemplate; however, several optional templates can be used to customize the appearance of the list.

AlternatingItemTemplate: If defined, provides the content and layout for alternating items in the DataList. If not defined, ItemTemplate is used.

EditItemTemplate: If defined, provides the content and layout for the item currently being edited in the DataList. If not defined, ItemTemplate is used.

FooterTemplate: If defined, provides the content and layout for the footer section of the DataList. If not defined, a footer section will not be displayed.

HeaderTemplate: If defined, provides the content and layout for the header section of the DataList. If not defined, a header section will not be displayed.

ItemTemplate: Required template that provides the content and layout for items in the DataList.

SelectedItemTemplate: If defined, provides the content and layout for the currently selected item in the DataList. If not defined, ItemTemplate is used.

SeparatorTemplate: If defined, provides the content and layout for the separator between items in the DataList. If not defined, a separator will not be displayed.

At the very minimum, the ItemTemplate needs to be defined to display the items in the DataList control. Additional templates can be used to provide a custom look to the DataList control.


The appearance of the DataList control may be customized by setting the style properties for the different parts of the control. The following table lists the different style properties.

AlternatingItemStyle: Specifies the style for alternating items in the DataList control.

EditItemStyle: Specifies the style for the item being edited in the DataList control.

FooterStyle: Specifies the style for the footer in the DataList control.

HeaderStyle: Specifies the style for the header in the DataList control.

ItemStyle: Specifies the style for the items in the DataList control.

SelectedItemStyle: Specifies the style for the selected item in the DataList control.

SeparatorStyle: Specifies the style for the separator between the items in the DataList control.

You can also show or hide different parts of the control. The following table lists the properties that control which parts are shown or hidden.

ShowFooter: Shows or hides the footer section of the DataList control.

ShowHeader: Shows or hides the header section of the DataList control.

The display direction of a DataList control can be vertical or horizontal. Set the RepeatDirection property to specify the display direction.
The layout of the DataList control is controlled with the RepeatLayout property. Setting this property to RepeatLayout.Table will display the DataList in a table format, while RepeatLayout.Flow displays the DataList without a table structure.
<asp:DataList ID=" dlMyCountry" runat="server">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Country_Code") %>'></asp:Label>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Country_Name") %>'></asp:Label>
</ItemTemplate>
</asp:DataList>

private void BindGrid()
{
string sql = "Select * from Country Order By Country_Name";
SqlDataAdapter da = new SqlDataAdapter(sql, “Yourconnectionstring”);
DataTable dt = new DataTable();
da.Fill(dt);

dlMyCountry.DataSource = dt;
dlMyCountry.DataBind();
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}

The DataList, DataGrid and Repeater control are similar in their behavior; they all connect to a data source from which they retrieve data to display as a repeated list of items. The DataList control uses HTML tables to format the data provided by the data source. The data source can be a SQL database, an XML file or an array.
Older Posts Home

ASP.NET Examples

 
2012 24x7 Magazine. All rights reserved.
Designed by 24x7 Magazine