Description
The following article is about techniques for editing, deleting, sorting and paging using web Datagrid control. For this purpose, I used NortWind database in MSDE. The program has two parts :
- ASPX file holds html code
- Code Behind C# class file, holds all the logic and methods.
Source Code:
ASPX file starts here: Here is the design of DataGrid object, I commented out what properties and events do, so it becomes easier to follow
<asp:DataGrid id="MyDataGrid" style="Z-INDEX: 101; LEFT: 16px; POSITION:
absolute; TOP: 104px" runat="server" BorderStyle="Ridge" GridLines="None"
BorderWidth="2px" BorderColor="White" BackColor="White" CellPadding="3"
CellSpacing="1" AllowPaging="True" AllowSorting="True" PageSize="25"
PagerStyle-Mode="NextPrev" PagerStyle-NextPageText="Next" PagerStyle-
PrevPageText="Previous" PagerStyle-HorizontalAlign="Center" PagerStyle-
Position="TopAndBottom" DataKeyField="ProductID"
OnPageIndexChanged="MyDataGrid_PageIndexChanged"
OnSortCommand="Sort_Grid" OnDeleteCommand="MyDataGrid_Delete"
OnUpdateCommand="MyDataGrid_Update"
OnCancelCommand="MyDataGrid_Cancel" OnEditCommand="MyDataGrid_Edit"
AutoGenerateColumns="False" HorizontalAlign="Left">
<FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
<HeaderStyle Font-Bold="True" ForeColor="#E7E7FF"
BackColor="#4A3C8C"></HeaderStyle>
<PagerStyle NextPageText="Next" PrevPageText="Previous"
HorizontalAlign="Right" ForeColor="Black" Position="TopAndBottom"
BackColor="#C6C3C6"></PagerStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#9471DE"></SelectedItemStyle>
<ItemStyle ForeColor="Black" BackColor="#DEDFDE"></ItemStyle>
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="<img
border=0 src=ok.gif>" CancelText="<img border=0 src=cancel.gif>"
EditText="<img border=0 src=edit.gif>"></asp:EditCommandColumn>
<asp:ButtonColumn Text="<img border=0 src=delete.gif>"
CommandName="Delete"></asp:ButtonColumn>
<asp:BoundColumn DataField="ProductID" SortExpression="ProductID"
ReadOnly="True" HeaderText="Product ID"></asp:BoundColumn>
<asp:BoundColumn DataField="ProductName" SortExpression="ProductName"
HeaderText="Product Name"></asp:BoundColumn>
<asp:BoundColumn DataField="QuantityPerUnit"
SortExpression="QuantityPerUnit" HeaderText="Quantity Per Unit">
</asp:BoundColumn>
<asp:BoundColumn DataField="UnitPrice" SortExpression="UnitPrice"
HeaderText="Unit Price" DataFormatString="{0:c}"></asp:BoundColumn>
<asp:BoundColumn DataField="UnitsInStock" SortExpression="UnitsInStock"
HeaderText="Units In Stock"></asp:BoundColumn>
<asp:BoundColumn DataField="UnitsOnOrder" SortExpression="UnitsOnOrder"
HeaderText="Units On Order"></asp:BoundColumn>
<asp:BoundColumn DataField="ReorderLevel" SortExpression="ReorderLevel"
HeaderText="Reorder Level"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Discontinued"
SortExpression="Discontinued">
<ItemTemplate>
<asp:CheckBox id="Discontinued" runat="server" Checked='<%#
DataBinder.Eval(Container.DataItem, "Discontinued") %>' />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
C# Code Behind starts here:
It is a good technique that you can bind data when the page is not postback,what that means that there will be one time data binding when the page is requested once.
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}
}
/// <summary>
/// this function returns dataset of products details
/// </summary>
/// <returns></returns>
private DataSet GetProductData()
{
///this is sql statement which returns records
String SQLStatement="SELECT Products.ProductID, Products.ProductName, Products.QuantityPerUnit,
Products.UnitPrice, "+ "Products.UnitsInStock, Products.UnitsOnOrder, Products.ReorderLevel,
Products.Discontinued "+ "FROM Products";
///instantiate sql connection and command object
SqlConnection myConnection= new SqlConnection(@"server=(local)
\NetSDK;database=NorthWind;uid=northwind;pwd=northwind;");
SqlDataAdapter myCommand = new SqlDataAdapter(SQLStatement,myConnection);
///declare myDataSet as DataSet
DataSet myDataSet;
// mark the Command as s Text
myCommand.SelectCommand.CommandType=CommandType.Text;
///create an instance of dataset
myDataSet = new DataSet();
///fill the dataset with returned records
myCommand.Fill(myDataSet, "Products");
Jithesh pPosted Jun 21, 2007, 7:07 AM
Can you tell me how to delete the records from gridview in c# code in visual studio 2005