Monday, 19 March 2012

Gridview Order In asp.net

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script  type="text/javascript">
     
     function emptytxtboxvalidation(a,b,c)
     {
       
                if(document.getElementById(a).value=="")
                {
                     alert("enter student id");
                      document.getElementById(a).focus();
                     return false;
                }
                    
               if(document.getElementById(b).value=="")
                {
                     alert("enter student name");
                      document.getElementById(b).focus();
                    return false;
                }
                
              if(document.getElementById(c).value=="")
                {
                    alert("enter student marks");
                      document.getElementById(c).focus();
                      return false;
                } 
                if (document.getElementById(a).value!="" || document.getElementById(b).value!="" ||document.getElementById(c).value!="" )
                {
                    alert("Correct data-You can Now Insert the data");
                    return false;
                }
 
            return true;

     }
    
    
    </script>
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <h2 style="color: blue; font-style: normal">
        Order gridview
        <br />
        in asp.net
    </h2>
    <div style="text-align: center">
        <table style="height: 450px; width: 616px">
            <tr>
                <td>
                    <table>
                        <tr>
                            <td>
                                <asp:LinkButton ID="lnkAddNewRow" runat="server" OnClick="lnkAddNewRow_Click">AddNewRow</asp:LinkButton>
                                <asp:Button ID="btnIns" runat="server" Text="Insert" CommandName="Insert" OnClick="btnIns_Click" />
                                <asp:Button ID="btnvalidate" runat="server" Text="Validate" />
                                <asp:Button ID="btnCancelInsert" runat="server" Text="Cancel Insertion" Visible="false"
                                    OnClick="btnCancelInsert_Click" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td>
                    <table width="500px" style="height: 327px">
                        <tr>
                            <td>
                                <asp:GridView ID="grdOrdering" runat="server" DataKeyNames="sid" AutoGenerateColumns="False"
                                    Width="555px" OnRowDataBound="grdOrdering_RowDataBound" OnRowCommand="grdOrdering_RowCommand">
                                    <Columns>
                                        <asp:TemplateField>
                                            <ItemTemplate>
                                                <asp:Button ID="BtnDel" runat="server" Text="Delete" CommandName="DeleteOne" />
                                                <asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="UpdateOne" Visible="false" />
                                                <asp:Button ID="BtnCancel" runat="server" Text="Cancel" CommandName="CancelOne" Visible="false" />
                                                <asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="EditOne" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField>
                                            <HeaderTemplate>
                                                Student ID<asp:TextBox ID="tid" runat="server" Visible="false"></asp:TextBox>
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <asp:Label ID="stuid" runat="server" Text=' <%#Eval("sid")%>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField>
                                            <HeaderTemplate>
                                                Student Name<asp:TextBox ID="tn" runat="server" Visible="false"></asp:TextBox>
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <asp:TextBox ID="txtname" runat="server" Text=' <%#Eval("sname")%>' Visible="false"></asp:TextBox>
                                                <asp:Label ID="stuname" runat="server" Text=' <%#Eval("sname")%>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField>
                                            <HeaderTemplate>
                                                Marks<asp:TextBox ID="tm" runat="server" Visible="false"></asp:TextBox>
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <asp:TextBox ID="txtmrks" runat="server" Text=' <%#Eval("smarks")%>' Visible="false"></asp:TextBox>
                                                <asp:Label ID="stumarks" runat="server" Text=' <%#Eval("smarks")%>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Order">
                                            <ItemTemplate>
                                                <asp:ImageButton ID="imbup" runat="server" ImageUrl="~/Images/a-icon-alt-arrow-up.png"
                                                    CommandName="moveup" />
                                                <asp:ImageButton ID="imbdown" runat="server" ImageUrl="~/Images/a-icon-alt-arrow-down.png"
                                                    CommandName="movedown" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                </asp:GridView>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
        <asp:Label ID="lblmsg" runat="server" Text="Label" Visible="false" ForeColor="Green"
            Font-Bold="true"></asp:Label>
    </div>
    </form>
</body>
</html>

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["studentmarks"].ToString());
    DataSet ds = new DataSet();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            bind();
        lblmsg.Visible = false;
        btnIns.Visible = false;
        btnvalidate.Visible = false;
        btnCancelInsert.Visible = false;
        displayGridNormally();
    }

    protected void bind()
    {
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from studentmarks order by recordorder", con);
        da.Fill(ds);
        grdOrdering.DataSource = ds;
        grdOrdering.DataBind();
        con.Close();
    }

    protected int getRowsCount()
    {
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from studentmarks order by recordorder", con);
        da.Fill(ds);
        con.Close();
        return ds.Tables[0].Rows.Count;
    }

    protected void displayGridNormally()
    {
        for (int i = 0; i < grdOrdering.Rows.Count; i++)
        {
            TextBox tname = (TextBox)grdOrdering.Rows[i].FindControl("txtname");
            TextBox tmarks = (TextBox)grdOrdering.Rows[i].FindControl("txtmrks");
            tname.Visible = false;
            tmarks.Visible = false;
            Label lblname = (Label)grdOrdering.Rows[i].FindControl("stuname");
            Label lblmarks = (Label)grdOrdering.Rows[i].FindControl("stumarks");
            lblname.Visible = true;
            lblmarks.Visible = true ;
            Button btnupdate = (Button)grdOrdering.Rows[i].Cells[0].FindControl("btnUpdate");
            btnupdate.Visible = false;
            Button btnedit = (Button)grdOrdering.Rows[i].Cells[0].FindControl("btnEdit");
            btnedit.Visible =true;
            Button btncancel = (Button)grdOrdering.Rows[i].Cells[0].FindControl("BtnCancel");
            btncancel.Visible = false;
            Button btndelete = (Button)grdOrdering.Rows[i].Cells[0].FindControl("BtnDel");
            btndelete.Visible = true;
        }
    }

    protected void grdOrdering_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button btndel = (Button)e.Row.FindControl("BtnDel"); //Use the button id of your button
            if (btndel != null)
            {
                btndel.CommandArgument = e.Row.RowIndex.ToString();
            }
            Button btnupdate = (Button)e.Row.FindControl("btnUpdate");
            if (btnupdate != null)
            {
                btnupdate.CommandArgument = e.Row.RowIndex.ToString();
            }
            Button btnedit = (Button)e.Row.FindControl("btnEdit");
            if (btnedit != null)
            {
                btnedit.CommandArgument = e.Row.RowIndex.ToString();
            }
            Button btncancel = (Button)e.Row.FindControl("BtnCancel");
            if (btncancel != null)
            {
                btncancel.CommandArgument = e.Row.RowIndex.ToString();
            }
            ImageButton imbup = (ImageButton)e.Row.FindControl("imbup");
            if (imbup != null)
            {
                imbup.CommandArgument = e.Row.RowIndex.ToString();
                if (Convert.ToInt32(e.Row.RowIndex.ToString()) == 0)
                    imbup.Visible = false;
            }
            ImageButton imbdown = (ImageButton)e.Row.FindControl("imbdown");
            if (imbdown != null)
            {
                imbdown.CommandArgument = e.Row.RowIndex.ToString();
                if (Convert.ToInt32(e.Row.RowIndex.ToString()) == (ds.Tables[0].Rows.Count - 1))
                    imbdown.Visible = false;
            }

        }
    }
    protected void grdOrdering_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToString() == "DeleteOne")
        {
            int rowIndex = int.Parse(e.CommandArgument.ToString());
            DataKey dk = grdOrdering.DataKeys[rowIndex];
            int sid = int.Parse(dk.Values["sid"].ToString());
            con.Open();
            SqlCommand cmd = new SqlCommand("delete from studentmarks where sid='" + sid + "'", con);
            cmd.ExecuteNonQuery();
            con.Close();
            lblmsg.Visible = true;
            lblmsg.Text = "Deleted Successfully";
            bind();
            
        } 
        else if (e.CommandName.ToString() == "moveup")
        {
            int rowIndex = int.Parse(e.CommandArgument.ToString());
            DataKey dk1 = grdOrdering.DataKeys[rowIndex];
            int sid1 = int.Parse(dk1.Values["sid"].ToString());
            DataKey dk2 = grdOrdering.DataKeys[rowIndex - 1];
            int sid2 = int.Parse(dk2.Values["sid"].ToString());
            con.Open();
            SqlCommand cmd = new SqlCommand("update studentmarks set recordorder="+(rowIndex - 1)+" where sid="+sid1, con);
            cmd.ExecuteNonQuery();
            cmd = new SqlCommand("update studentmarks set recordorder="+rowIndex+" where sid="+sid2, con);
            cmd.ExecuteNonQuery();
            con.Close();
            bind();
            
        }
        else if (e.CommandName.ToString() == "movedown")
        {
            int rowIndex = int.Parse(e.CommandArgument.ToString());
            DataKey dk1 = grdOrdering.DataKeys[rowIndex];
            int sid1 = int.Parse(dk1.Values["sid"].ToString());
            DataKey dk2 = grdOrdering.DataKeys[rowIndex+1];
            int sid2 = int.Parse(dk2.Values["sid"].ToString());
            con.Open();
            SqlCommand cmd = new SqlCommand("update studentmarks set recordorder=" + (rowIndex + 1) + " where sid=" + sid1, con);
            cmd.ExecuteNonQuery();
            cmd = new SqlCommand("update studentmarks set recordorder=" + rowIndex + " where sid=" + sid2, con);
            cmd.ExecuteNonQuery();
            con.Close();
            bind();
        }
        else if (e.CommandName.ToString() == "UpdateOne")
        {
            int rowIndex = int.Parse(e.CommandArgument.ToString());
            DataKey dk = grdOrdering.DataKeys[rowIndex];
            int sid = int.Parse(dk.Values["sid"].ToString());
            TextBox tname = (TextBox)grdOrdering.Rows[rowIndex].FindControl("txtname");
            TextBox tmarks = (TextBox)grdOrdering.Rows[rowIndex].FindControl("txtmrks");
            con.Open();
            SqlCommand cmd = new SqlCommand("update studentmarks set sname='" + tname.Text + "',smarks='" + tmarks.Text + "' where sid=" + sid, con);
            cmd.ExecuteNonQuery();
            con.Close();
            grdOrdering.EditIndex = -1;
            lblmsg.Visible = true;
            lblmsg.Text = "updated Successfully";
            bind();
        }
        else if (e.CommandName.ToString() == "EditOne")
        {
            int rowIndex = int.Parse(e.CommandArgument.ToString());
            TextBox tname = (TextBox)grdOrdering.Rows[rowIndex].FindControl("txtname");
            TextBox tmarks = (TextBox)grdOrdering.Rows[rowIndex].FindControl("txtmrks");
            tname.Visible = true;
            tmarks.Visible = true;
            Label lblname = (Label)grdOrdering.Rows[rowIndex].FindControl("stuname");
            Label lblmarks = (Label)grdOrdering.Rows[rowIndex].FindControl("stumarks");
            lblname.Visible = false;
            lblmarks.Visible = false;
            tname.Text = lblname.Text;
            tmarks.Text = lblmarks.Text;
            Button btnupdate = (Button)grdOrdering.Rows[rowIndex].Cells[0].FindControl("btnUpdate");
            btnupdate.Visible = true;
            Button btnedit = (Button)grdOrdering.Rows[rowIndex].Cells[0].FindControl("btnEdit");
            btnedit.Visible = false;
            Button btncancel = (Button)grdOrdering.Rows[rowIndex].Cells[0].FindControl("BtnCancel");
            btncancel.Visible = true;
            Button btndelete = (Button)grdOrdering.Rows[rowIndex].Cells[0].FindControl("BtnDel");
            btndelete.Visible = false;
        }
        else if (e.CommandName.ToString() == "CancelOne")
        {
            grdOrdering.EditIndex = -1;
        }


    }

   
    protected void btnIns_Click(object sender, EventArgs e)
    {
        btnIns.Visible = true;
        TextBox sid = grdOrdering.HeaderRow.FindControl("tid") as TextBox;
        TextBox sname = grdOrdering.HeaderRow.FindControl("tn") as TextBox;
        TextBox smarks = grdOrdering.HeaderRow.FindControl("tm") as TextBox;
        btnvalidate.Attributes.Add("OnClick", "return emptytxtboxvalidation('" + Convert.ToString(sid.ClientID) + "','" + Convert.ToString(sname.ClientID) + "','" + Convert.ToString(smarks.ClientID) + "');");
        if (sid.Text != "" && sname.Text != "" && smarks.Text != "")
        {
            btnvalidate.Visible = false;
            int rowcount = getRowsCount();            
            con.Open();
            string str1 = "insert into studentmarks values (' " + sid.Text + " ' , ' " + sname.Text + " ', ' " + smarks.Text + " ','" + (rowcount - 1) + "')";
            SqlCommand cmd = new SqlCommand(str1, con);
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
            con.Close();
            lnkAddNewRow.Visible = true;
            lblmsg.Visible = true;
            lblmsg.Text = "Data Inserted Successfully";
            bind();
            btnIns.Visible = false;
        }
        else
        {
            btnvalidate.Visible = true;
            btnvalidate.Focus();

        }

    }


    protected void lnkAddNewRow_Click(object sender, EventArgs e)
    {
        TextBox t1 = grdOrdering.HeaderRow.FindControl("tid") as TextBox;
        TextBox t2 = grdOrdering.HeaderRow.FindControl("tn") as TextBox;
        TextBox t3 = grdOrdering.HeaderRow.FindControl("tm") as TextBox;
        t1.Visible = true;
        t2.Visible = true;
        t3.Visible = true;
        btnCancelInsert.Visible = true;
        lnkAddNewRow.Visible = false;
        btnIns.Visible = true;
    }

    protected void btnCancelInsert_Click(object sender, EventArgs e)
    {
        lnkAddNewRow.Visible = true;
        btnIns.Visible = false;
        btnvalidate.Visible = false;
        btnCancelInsert.Visible = false;
        TextBox t1 = grdOrdering.HeaderRow.FindControl("tid") as TextBox;
        TextBox t2 = grdOrdering.HeaderRow.FindControl("tn") as TextBox;
        TextBox t3 = grdOrdering.HeaderRow.FindControl("tm") as TextBox;
        t1.Visible = false;
        t2.Visible = false;
        t3.Visible = false;
        
    }
}

No comments:

Post a Comment