Monday, November 17, 2008

GridView DeleteAll,SelectAll,ClearAll(Server Side)

0 comments 11/17/2008
Sign up to receive our site updates!

The Following Example Demonstrates on SelectAll,DeleteAll and CleareAll

Here SelectAll and ClearAll button Click Events run at server......



In Coding Page



<asp:Button ID="btnselectall" runat="server" OnClick="btnselectall_Click" Style="z-index: 100;

left: 225px; position: absolute; top: 112px" Text="SelectAll" />

<asp:Button ID="btnclearall" runat="server" OnClick="btnclearall_Click" Style="z-index: 101;

left: 311px; position: absolute; top: 112px" Text="ClearAll" />

<asp:Button ID="btndelete" runat="server" OnClick="btndelete_Click" Style="z-index: 102;

left: 388px; position: absolute; top: 112px" Text="DeleteAll" />

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"

Style="z-index: 104; left: 225px; position: absolute; top: 140px" AutoGenerateColumns="False" DataKeyNames="eno">

<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

<RowStyle BackColor="#EFF3FB" />

<EditRowStyle BackColor="#2461BF" />

<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

<AlternatingRowStyle BackColor="White" />

<Columns>

<asp:TemplateField >



<ItemTemplate>

<asp:CheckBox ID="chk1" runat="server" />

ItemTemplate>

<HeaderStyle HorizontalAlign="Left" />

<ItemStyle HorizontalAlign="Left" />

asp:TemplateField>

<asp:BoundField DataField="eno" HeaderText="EmpNo" ReadOnly="True" SortExpression="eno" />

<asp:BoundField DataField="ename" HeaderText="ename" SortExpression="ename" />

<asp:BoundField DataField="sal" HeaderText="sal" SortExpression="sal" />

Columns>

asp:GridView>



Set Following GridView Properties

DatakeyNames=eno(set Primary Key colume name)

In .CS File

using System.Data.Odbc;

public partial class _Default : System.Web.UI.Page

{

string str;

OdbcConnection myConn = new OdbcConnection("driver={Mysql odbc 3.51 driver};user id=root;password=admin;server=localhost;database=temp");

protected void Page_Load(object sender, EventArgs e)

{

if (IsPostBack == false)

{

str = "select * from emp1";

getdata(str);

}

}

public void getdata(string s)

{

OdbcDataAdapter da = new OdbcDataAdapter(s, myConn);

DataSet ds = new DataSet();

da.Fill(ds, "raji");

GridView1.DataSource = ds;

GridView1.DataBind();

}





protected void btnselectall_Click(object sender, EventArgs e)

{

foreach (GridViewRow r in GridView1.Rows)

{

CheckBox cb = (CheckBox)r.FindControl("chk1");

cb.Checked = true;

}

}

protected void btnclearall_Click(object sender, EventArgs e)

{

foreach (GridViewRow r in GridView1.Rows)

{

CheckBox cb = (CheckBox)r.FindControl("chk1");

cb.Checked = false;

}

}

protected void btndelete_Click(object sender, EventArgs e)

{

string ds = "";

foreach (GridViewRow r in GridView1.Rows)

{

CheckBox cb = (CheckBox)r.FindControl("chk1");

if (cb.Checked)

{

int eno = Convert.ToInt32(GridView1.DataKeys[r.RowIndex].Value);

ds = "delete from emp1 where eno=" + eno;

OdbcCommand cmd = new OdbcCommand(ds, myConn);

myConn.Open();

cmd.ExecuteNonQuery();

myConn.Close();

}

}

getdata("select * from emp1");

}

}

Your Ad Here

0 comments:

Respects for your's Questions & Opinions

  • Friends
  •  

    Recent Posts

    New Comments

    Copyright 2008 All Rights Reserved Tech Tasks Template by Rajesh Kumar Chekuri

    Share
    Me
    Twitter Facebook Google +1 Pinterest Delicious Digg Stumbleupon More

    Connect to TT Facebook Google +1
    Back to Top