Sunday, 15 July 2012

Gridview control with datatable

protected void Page_Load(object sender, EventArgs e)
  {

    if (!Page.IsPostBack)
    {
      // Create a new table.
      DataTable taskTable = new DataTable("TaskList");

      // Create the columns.
      taskTable.Columns.Add("Id", typeof(int));
      taskTable.Columns.Add("
Description", typeof(string));
      taskTable.Columns.Add("IsComplete", typeof(bool) );

      //Add data to the new table.
      for (int i = 0; i < 20; i++)
      {
        DataRow tableRow = taskTable.NewRow();
        tableRow["Id"] = i;
        tableRow["Description"] = "Task " + i.ToString();
        tableRow["IsComplete"] = false;           
        taskTable.Rows.Add(tableRow);
      }

      //Persist the table in the Session object.
      Session["TaskTable"] = taskTable;

      //Bind data to the GridView control.
      BindData();
    }

  }

  protected void TaskGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
    TaskGridView.PageIndex = e.NewPageIndex;
    //Bind data to the GridView control.
    BindData();
  }

  protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
  {
    //Set the edit index.
    TaskGridView.EditIndex = e.NewEditIndex;
    //Bind data to the GridView control.
    BindData();
  }

  protected void TaskGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  {
    //Reset the edit index.
    TaskGridView.EditIndex = -1;
    //Bind data to the GridView control.
    BindData();
  }

  protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {   
    //Retrieve the table from the session object.
    DataTable dt = (DataTable)Session["TaskTable"];

    //Update the values.
    GridViewRow row = TaskGridView.Rows[e.RowIndex];
    dt.Rows[row.DataItemIndex]["Id"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["Description"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["IsComplete"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked;

    //Reset the edit index.
    TaskGridView.EditIndex = -1;

    //Bind data to the GridView control.
    BindData();
  }

  private void BindData()
  {
    TaskGridView.DataSource = Session["TaskTable"];
    TaskGridView.DataBind();
  }
__________________________________________________________________________________________
protected void gvShowCategory_RowEditing(object sender, GridViewEditEventArgs e)

    {
        oCCategory = new CCategory();

        Session["CategoryID"] = Convert.ToInt32(gvShowCategory.DataKeys[e.NewEditIndex].Value);

        oCCategory.CategoryID = Convert.ToInt16(Session["CategoryID"]);

        //this method is used to Get Record from Translation_Category on the Basis of CategoryID

        if (oCCategory.GetCategoryDetailByCategoryID())

        {
            txtCategoryName.Text = Convert.ToString(oCCategory.CategoryName);
            txtCategoryDescription.Text = Convert.ToString(oCCategory.CategoryDescription);
       }

    }

    protected void gvShowCategory_RowCommand(object sender, GridViewCommandEventArgs e)

    {
        oCCategory = new CCategory();

        if (e.CommandName == "Delete")
        {
            oCCategory.CategoryID = Convert.ToInt32(e.CommandArgument);
            //This Method is used to Delete Particular Record On the Basis of CategoryID

            if (oCCategory.CheckDependences())
            {
                if (!oCCategory.Dependences)
                {
                    if (oCCategory.DeleteOldCategory())
                    {
                        Session["CategoryID"] = null;
                        txtCategoryName.Text = "";
                        txtCategoryDescription.Text = "";
                        oCCategory.ShowAllCategory(ref gvShowCategory, sortExpression, direction);
                        lblMsg.Text = "Your Record Delete Sucessfully!";

                        //  ClientScript.RegisterStartupScript(e.GetType(), "Message", "<script>alert('Your Record Delete Sucessfully !')</script>");
                        return;
                    }
                }

                else
                {
                    oCCategory.ShowAllCategory(ref gvShowCategory, sortExpression, direction);
                    lblMsg.Text = "Can't Delete This Record Because It is used in other Place!";

                    // ClientScript.RegisterStartupScript(e.GetType(), "Message", "<script>alert('Can't Delete This Record Because It is used in other Place !')</script>");

                    // return;
                }
            }
        }
    }

    protected void gvShowCategory_Sorting(object sender, GridViewSortEventArgs e)
    {
        oCCategory = new CCategory();
        string sortExpression = e.SortExpression;
        ViewState["sortExpression"] = e.SortExpression;
        //  string direction = "ASC";
        if (GridViewSortDirection == SortDirection.Ascending)
        {
            direction = "ASC";
            //GridViewSortDirection = SortDirection.Descending;
        }
        else
        {
            direction = "DESC";
            // GridViewSortDirection = SortDirection.Ascending;
        }
        oCCategory.ShowAllCategory(ref gvShowCategory, sortExpression, direction);
    }

   public SortDirection GridViewSortDirection
    {

        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Ascending;

            return (SortDirection)ViewState["sortDirection"];
        }
        set
        {
            ViewState["sortDirection"] = value;
        }
    }
_______________________________________________________________________________________
<asp:GridView
        id="grdMovies"
        DataSourceID="srcMovies"
        DataKeyNames="Id"
        AutoGenerateColumns="false"
        AutoGenerateEditButton="true"
        Runat="server">
        <Columns>
        <asp:TemplateField HeaderText="Title">
            <ItemTemplate>
            <%# Eval("Title") %>
            </ItemTemplate>
            <EditItemTemplate>
            <asp:TextBox
                id="txtTitle"
                Text='<%# Bind("Title") %>'
                Runat="server" />
            <asp:RequiredFieldValidator
                id="valTitle"
                ControlToValidate="txtTitle"
                Text="(required)"
                Runat="server" />   
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Category">
            <ItemTemplate>
            <%# Eval("Name") %>
            </ItemTemplate>
            <EditItemTemplate>
            <asp:DropDownList
                id="ddlCategory"
                DataSourceID="srcMovieCategories"
                DataTextField="Name"
                DataValueField="Id"
                SelectedValue='<%# Bind("CategoryId") %>'
                Runat="server" />
            </EditItemTemplate>
        </asp:TemplateField>
        </Columns>
    </asp:GridView>
___________________________________________________________________________________________
Acess Multiple DataKeys:----------
--------------------------------sourabh---------------------
gdvApplications.DataKeys[gdvApplications.SelectedIndex].Values["name"];
gdvApplications.DataKeys[gdvApplications.SelectedIndex].Values["version"];
--------------------------------sourabh---------------------
    protected void OnRowCommand_ShowQuery(Object sender,GridViewCommandEventArgs e)
    {
        // If multiple buttons are used in a GridView control, use the
        // CommandName property to determine which button was clicked.
        if (e.CommandName == "showQueryCommand")
        {
            // Convert the row index stored in the CommandArgument
            // property to an Integer.
            int index = Convert.ToInt32(e.CommandArgument);

            // Retrieve the row that contains the button clicked
            // by the user from the Rows collection.
            DataKey dk = this.queryGridControl.DataKeys[index];
            int query_id = Convert.ToInt32(dk.Value);
        }
    }
-----------------------------------------------------
Private Sub GridView1_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles GridView1.SelectedIndexChanging
        Dim po As Integer = CInt(GridView1.DataKeys(e.NewSelectedIndex)("PO"))
        Dim VendorID As Integer = CInt(GridView1.DataKeys(e.NewSelectedIndex)("VendorID"))


    End Sub
-----------------------------------------------------
protected void GridView1_SelectedIndexChanged2
               (object sender, EventArgs e)
string App_id =
GridView1.DataKeys[GridView1.SelectedIndex]
["ResponseID"].ToString();
SqlDataSource3.SelectParameters.Clear();
SqlDataSource3.SelectParameters.Add
("ResponseID", App_id);
DetailsView2.DataBind();
string Source_id =
GridView1.DataKeys[GridView1.SelectedIndex]
["Source_id"].ToString();
SqlDataSource4.SelectParameters.Clear();
SqlDataSource4.SelectParameters.Add
("Source_id", Source_id);
DetailsView3.DataBind();
    }
  -----------------------------------------------------
gvwCourseStatus.DataKeys.Item(0).Value
-----------------------------------------------------
//Find Rows

       protected void gvas_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        objCls_Category = new Cls_Category();
        int ID = (int)gvas.DataKeys[e.RowIndex].Value;           
      GridViewRow row = gvas.Rows[e.RowIndex];
            if (row != null)
            {
                TextBox t = row.FindControl("txtCatNmae") as TextBox;              
            }     
        gvas.EditIndex = -1;     
     
        objCls_Category.ShowAllCategory(ref gvas);
    }

No comments:

Post a Comment