DotNet Academy of Rajesh Rolen

Solutions by Rajesh Rolen

Showing posts with label GridView. Show all posts
Showing posts with label GridView. Show all posts

Basic Java Script Examples


lets we have a gridview as shown in pic. now we want have got rates of room and we want to select quantity in dropdownlist and on that basis we want to calculate total in last column of gridview. and then grand total in label below gridview.
then below javascript will help u out:





< script language="javascript" type="text/javascript" >
function ShowTotalPrices(selectbox) {

// var selectedStatus = dropDown.value;
//alert(selectedStatus);
var parentTr = selectbox.parentNode.parentNode;
var secondCellVal = parseInt ( parentTr.cells[4].innerText , 10 );
var comboVal = parseInt ( selectbox.value , 10 );

parentTr.cells[7].innerText = secondCellVal * comboVal;
ManipulateGrid();
}

function ManipulateGrid()
{
var gvDrv = document.getElementById("< %= GridView1.ClientID % >");
var gt=0.0;
for (i=1; i< gvDrv.rows.length; i++)
{
var cell = gvDrv.rows[i].cells;
var valold = cell[7].innerHTML;
var val = 0.0;
if(isNaN(parseFloat(valold)))
{
val= 0.0;
}
else
{
val = parseFloat(valold);
}

gt = parseFloat (gt) + val;
}
document.getElementById("lblgtamount").innerText=gt;
//lbl.Text = gt;
//alert(gt);
}

< /script >



in codding file write below code:
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 ddlroombook As DropDownList = CType(e.Row.FindControl("ddlbookroom"), DropDownList)
Dim availablerooms As Integer = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "NumberOfUnits"))
ddlroombook.Attributes.Add("onchange", "javascript:ShowTotalPrices(this);")
Dim i As Integer
For i = 0 To availablerooms
ddlroombook.Items.Add(i)
Next
End If
End Sub

Basic Java Script Examples


lets we have a gridview as shown in pic. now we want have got rates of room and we want to select quantity in dropdownlist and on that basis we want to calculate total in last column of gridview. and then grand total in label below gridview.
then below javascript will help u out:





< script language="javascript" type="text/javascript" >
function ShowTotalPrices(selectbox) {

// var selectedStatus = dropDown.value;
//alert(selectedStatus);
var parentTr = selectbox.parentNode.parentNode;
var secondCellVal = parseInt ( parentTr.cells[4].innerText , 10 );
var comboVal = parseInt ( selectbox.value , 10 );

parentTr.cells[7].innerText = secondCellVal * comboVal;
ManipulateGrid();
}

function ManipulateGrid()
{
var gvDrv = document.getElementById("< %= GridView1.ClientID % >");
var gt=0.0;
for (i=1; i< gvDrv.rows.length; i++)
{
var cell = gvDrv.rows[i].cells;
var valold = cell[7].innerHTML;
var val = 0.0;
if(isNaN(parseFloat(valold)))
{
val= 0.0;
}
else
{
val = parseFloat(valold);
}

gt = parseFloat (gt) + val;
}
document.getElementById("lblgtamount").innerText=gt;
//lbl.Text = gt;
//alert(gt);
}

< /script >



in codding file write below code:
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 ddlroombook As DropDownList = CType(e.Row.FindControl("ddlbookroom"), DropDownList)
Dim availablerooms As Integer = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "NumberOfUnits"))
ddlroombook.Attributes.Add("onchange", "javascript:ShowTotalPrices(this);")
Dim i As Integer
For i = 0 To availablerooms
ddlroombook.Items.Add(i)
Next
End If
End Sub

Restricting numeric entries only in a DataGridView column

At times we have a requirement where we need to restrict the user to enter only numbers in a column of a DataGridView to achieve this we need to use EditingControlShowing event of DataGridView, this event is new in .Net framework 2.0 and it occurs when a control for editing a cell is showing. Following sample shows a implementation of it:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if ((int)(((System.Windows.Forms.DataGridView)(sender)).CurrentCell.ColumnIndex) == 1)
{
e.Control.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TextboxNumeric_KeyPress);

}
}

private void TextboxNumeric_KeyPress(object sender, KeyPressEventArgs e)
{
Boolean nonNumberEntered;

nonNumberEntered = true;

if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 8)
{
nonNumberEntered = false ;
}

if (nonNumberEntered == true)
{
// Stop the character from being entered into the control since it is non-numerical.
e.Handled = true ;
}
else
{
e.Handled = false;
}

}

Restricting numeric entries only in a DataGridView column

At times we have a requirement where we need to restrict the user to enter only numbers in a column of a DataGridView to achieve this we need to use EditingControlShowing event of DataGridView, this event is new in .Net framework 2.0 and it occurs when a control for editing a cell is showing. Following sample shows a implementation of it:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if ((int)(((System.Windows.Forms.DataGridView)(sender)).CurrentCell.ColumnIndex) == 1)
{
e.Control.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TextboxNumeric_KeyPress);

}
}

private void TextboxNumeric_KeyPress(object sender, KeyPressEventArgs e)
{
Boolean nonNumberEntered;

nonNumberEntered = true;

if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 8)
{
nonNumberEntered = false ;
}

if (nonNumberEntered == true)
{
// Stop the character from being entered into the control since it is non-numerical.
e.Handled = true ;
}
else
{
e.Handled = false;
}

}

How will decide the design consideration to take a Datagrid , datalist or repeater

Many make a blind choice of choosing datagrid directly , but that's not the right way.
Datagrid provides ability to allow the end-user to sort, page, and edit its data.But it
comes at a cost of speed.Second the display format is simple that is in row and columns.
Real life scenarios can be more demanding that With its templates, the DataList provides more control over the look and feel of the displayed data than the DataGrid.It offers better performance than datagrid Repeater control allows for complete and total control. With the Repeater, the only HTML emitted are the values of the databinding statements in the templates along with the HTML markup specified in the templates—no "extra" HTML is emitted, as with the DataGrid and DataList. By requiring the developer to specify the complete generated HTML markup, the Repeater often requires the longest development time.But repeater does not provide editing features like datagrid so everything has to be coded by programmer . However, the Repeater does boast the best performance of the three data Web controls.
Repeater is fastest followed by Datalist and finally datagrid.

How will decide the design consideration to take a Datagrid , datalist or repeater

Many make a blind choice of choosing datagrid directly , but that's not the right way.
Datagrid provides ability to allow the end-user to sort, page, and edit its data.But it
comes at a cost of speed.Second the display format is simple that is in row and columns.
Real life scenarios can be more demanding that With its templates, the DataList provides more control over the look and feel of the displayed data than the DataGrid.It offers better performance than datagrid Repeater control allows for complete and total control. With the Repeater, the only HTML emitted are the values of the databinding statements in the templates along with the HTML markup specified in the templates—no "extra" HTML is emitted, as with the DataGrid and DataList. By requiring the developer to specify the complete generated HTML markup, the Repeater often requires the longest development time.But repeater does not provide editing features like datagrid so everything has to be coded by programmer . However, the Repeater does boast the best performance of the three data Web controls.
Repeater is fastest followed by Datalist and finally datagrid.

What’s the method to customize columns in DataGrid

Use the template column.

How can we format data inside DataGrid

Use the DataFormatString property

How can we format data inside DataGrid

Use the DataFormatString property

What’s the method to customize columns in DataGrid

Use the template column.

Difference between Datagrid , Datalist and repeater From performance point of view

Repeater is fastest followed by Datalist and finally datagrid.

Difference between Datagrid , Datalist and repeater From performance point of view

Repeater is fastest followed by Datalist and finally datagrid.

What’s difference between Datagrid , Datalist and repeater

A Datagrid, Datalist and Repeater are all ASP.NET data Web controls.
They have many things in common like DataSource Property , DataBind Method
ItemDataBound and ItemCreated.
When you assign the DataSource Property of a Datagrid to a DataSet then each DataRow
present in the DataRow Collection of DataTable is assigned to a corresponding
DataGridItem and this is same for the rest of the two controls also.But The HTML code
generated for a Datagrid has an HTML TABLE element created for the particular
DataRow and its a Table form representation with Columns and Rows.
For a Datalist its an Array of Rows and based on the Template Selected and the
RepeatColumn Property value We can specify how many DataSource records should
appear per HTML < table > row. In short in datagrid we have one record per row, but in datalist we can have five or six rows per row.
For a Repeater Control,The Datarecords to be displayed depends upon the Templates
specified and the only HTML generated is the due to the Templates.
In addition to these , Datagrid has a in-built support for Sort,Filter and paging the Data ,which is not possible when using a DataList and for a Repeater Control we would require to write an explicit code to do paging.

What’s difference between Datagrid , Datalist and repeater

A Datagrid, Datalist and Repeater are all ASP.NET data Web controls.
They have many things in common like DataSource Property , DataBind Method
ItemDataBound and ItemCreated.
When you assign the DataSource Property of a Datagrid to a DataSet then each DataRow
present in the DataRow Collection of DataTable is assigned to a corresponding
DataGridItem and this is same for the rest of the two controls also.But The HTML code
generated for a Datagrid has an HTML TABLE element created for the particular
DataRow and its a Table form representation with Columns and Rows.
For a Datalist its an Array of Rows and based on the Template Selected and the
RepeatColumn Property value We can specify how many DataSource records should
appear per HTML < table > row. In short in datagrid we have one record per row, but in datalist we can have five or six rows per row.
For a Repeater Control,The Datarecords to be displayed depends upon the Templates
specified and the only HTML generated is the due to the Templates.
In addition to these , Datagrid has a in-built support for Sort,Filter and paging the Data ,which is not possible when using a DataList and for a Repeater Control we would require to write an explicit code to do paging.

Data into DataGridView from XML file

In this article we are going to know how to get data in DataGridView from XML file.
now we create an XML file.

Create an XML file:

< ?xml version="1.0" encoding="utf-8" ? >
< bookstore >
< book >
< title > The < /title >
< price > 8.99 < /price >
< genre > autobiograph < /genre >
< publicationdate > 1981 < /publicationdate >
< ISBN > 1-861003-11- < /ISBN >
< PageSize > 500 < /PageSize >
< /book >

< book >
< title > The Confiden < /title >
< price > 11.99 < /price >
< genre > novel < /genre >
< publicationdate > 1967 < /publicationdate >
< ISBN > 0-201-63361- < /ISBN >
< /book >

< book >
< title > The Gorgias < /title >
< price > 9.99 < /price >
< genre > philosophy < /genre >
< publicationdate > 1991 < /publicationdate >
< ISBN > ss1-861001-57- < /ISBN >
< PageSize > 600 < /PageSize >
< /book >
< /bookstore >

Create the window application:
There are the following steps for creating the window application.

Step 1: Create new project.

Step 2: Drag and drop DataGridView control from the toolbox on form, and fill all required properties.

Step 3: Add the following namespace to page (form):

using System.Data.SqlClient;
using System.Xml;

Step 4: Write the code on the form load event.

For Example:

Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Xml;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
XmlDataDocument xmlDatadoc = new XmlDataDocument();
xmlDatadoc.DataSet.ReadXml("C:\\books.xml");
DataSet ds = new DataSet("Books DataSet");
ds = xmlDatadoc.DataSet;
dataGridView1.DataSource = ds.DefaultViewManager;
dataGridView1.DataMember = "Book";
}
}
}

Data into DataGridView from XML file

In this article we are going to know how to get data in DataGridView from XML file.
now we create an XML file.

Create an XML file:

< ?xml version="1.0" encoding="utf-8" ? >
< bookstore >
< book >
< title > The < /title >
< price > 8.99 < /price >
< genre > autobiograph < /genre >
< publicationdate > 1981 < /publicationdate >
< ISBN > 1-861003-11- < /ISBN >
< PageSize > 500 < /PageSize >
< /book >

< book >
< title > The Confiden < /title >
< price > 11.99 < /price >
< genre > novel < /genre >
< publicationdate > 1967 < /publicationdate >
< ISBN > 0-201-63361- < /ISBN >
< /book >

< book >
< title > The Gorgias < /title >
< price > 9.99 < /price >
< genre > philosophy < /genre >
< publicationdate > 1991 < /publicationdate >
< ISBN > ss1-861001-57- < /ISBN >
< PageSize > 600 < /PageSize >
< /book >
< /bookstore >

Create the window application:
There are the following steps for creating the window application.

Step 1: Create new project.

Step 2: Drag and drop DataGridView control from the toolbox on form, and fill all required properties.

Step 3: Add the following namespace to page (form):

using System.Data.SqlClient;
using System.Xml;

Step 4: Write the code on the form load event.

For Example:

Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Xml;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
XmlDataDocument xmlDatadoc = new XmlDataDocument();
xmlDatadoc.DataSet.ReadXml("C:\\books.xml");
DataSet ds = new DataSet("Books DataSet");
ds = xmlDatadoc.DataSet;
dataGridView1.DataSource = ds.DefaultViewManager;
dataGridView1.DataMember = "Book";
}
}
}

Export Data Grid Content to an XML File

we can use the RenderControl method to extract the content into an .xml file from datagridview.
your Data Grid is not associated with any dataset.and not using any XML support functions

The following code explains:


ArrayList al = new ArrayList();
for(int i=0;i<=10;i++)
{
al.Add(i+"");
}
DataGrid1.DataSource=al;
DataGrid1.DataBind();
Response.Clear();
Response.AddHeader("content-disposition","fileattachment;filename=xmltest.xml");
Response.ContentType="text/xml";

StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
DataGrid1.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();

Export Data Grid Content to an XML File

we can use the RenderControl method to extract the content into an .xml file from datagridview.
your Data Grid is not associated with any dataset.and not using any XML support functions

The following code explains:


ArrayList al = new ArrayList();
for(int i=0;i<=10;i++)
{
al.Add(i+"");
}
DataGrid1.DataSource=al;
DataGrid1.DataBind();
Response.Clear();
Response.AddHeader("content-disposition","fileattachment;filename=xmltest.xml");
Response.ContentType="text/xml";

StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
DataGrid1.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();

About this blog

My Blog List

Advertise On This Site

Site Info

Advertise on this Site

To advertise on this site please mail on RajeshRolen@gmail.com

Information Source

About

Pages

Dot Net Academy

Advertis in This Area of Site

Powered by Blogger.

Followers

Search This Blog