GrpSMK Ответов: 0

Как редактировать строку при выборе значений ddl в gridview


В gridview я хочу отредактировать строку,в то время как я выбираю выпадающий список whish также в той же строке, а затем обновляю, как обычно.

Что я уже пробовал:

страница ASPX
<pre><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Gridviewcontrol._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">
    <title></title>
    <script type="text/javascript"  src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    </head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="gvdetail" runat="server" AllowPaging="True" DataKeyNames="productid,productname"
            AutoGenerateColumns="False" onpageindexchanging="gvdetail_PageIndexChanging" 
            onrowcancelingedit="gvdetail_RowCancelingEdit" 
            onrowcommand="gvdetail_RowCommand" onrowdeleting="gvdetail_RowDeleting" 
            onrowediting="gvdetail_RowEditing" onrowupdating="gvdetail_RowUpdating" 
            ShowFooter="True" onrowdatabound="gvdetail_RowDataBound" Width="688px" >
             
            
            <Columns>
            <asp:BoundField DataField="productid" HeaderText="ProductId" ReadOnly="True" />
            <asp:TemplateField HeaderText="ProductName">
            <ItemTemplate>
            <asp:Label ID="lblProductname" runat="server" Text='<%#Eval("productname") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
            <asp:TextBox ID="txtProductname" runat="server" Text='<%#Eval("productname") %>'></asp:TextBox>
            </EditItemTemplate>
            <FooterTemplate>
           <asp:TextBox ID="txtpname" runat="server"></asp:TextBox>
            </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Price">
            <ItemTemplate>
            <asp:Label ID="lblprice" runat="server"  Text='<%#Eval("price") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
            <asp:TextBox ID="txtprice" runat="server" Text='<%#Eval("price") %>'></asp:TextBox>
            </EditItemTemplate>
            <FooterTemplate>
            <asp:TextBox ID="txtprice" runat="server"></asp:TextBox>
            <asp:Button ID="btnAdd" CommandName="AddNew" runat="server" Text="Add"></asp:Button><br />
            </FooterTemplate>
            </asp:TemplateField>
            
            <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" />
            <asp:TemplateField HeaderText="DropDown">
            <ItemTemplate>
                <asp:DropDownList ID="DropDownList3" runat="server" AppendDataBoundItems="true">
                <asp:ListItem Value="-1">Select</asp:ListItem>
                </asp:DropDownList>
                 </ItemTemplate>
                
                <FooterTemplate>
                <asp:DropDownList  ID="DropDownList1"  runat="server" AppendDataBoundItems="true" AutoPostBack="True" >
            <asp:ListItem Value="-1">--Select--</asp:ListItem>
            
        </asp:DropDownList>

                </FooterTemplate>
               
                </asp:TemplateField>
            </Columns>
            </asp:GridView>
            
            <asp:Label ID="lblresult" runat="server"></asp:Label>

    </div>
    </form>
</body>
</html>


Aspx-файл.в CS
<pre>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;


namespace Gridviewcontrol
{
    public partial class _Default : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(@"user id=sa;password=ssa;database=Mohan;data source=PCTH101\PCTH101");
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                BindGridview();
            }
        }

        protected void BindGridview()
        {
            DataSet ds = new DataSet();
           
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("crudoperation", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@status", "SELECT");
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                con.Close();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    gvdetail.DataSource = ds;
                    gvdetail.DataBind();
                }
                else
                {
                    ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                    gvdetail.DataSource = ds;
                    gvdetail.DataBind();
                    int columncount = gvdetail.Rows[0].Cells.Count;
                    gvdetail.Rows[0].Cells.Clear();
                    gvdetail.Rows[0].Cells.Add(new TableCell());
                    gvdetail.Rows[0].Cells[0].ColumnSpan = columncount;
                    gvdetail.Rows[0].Cells[0].Text = "No Records Found";
                }
            }
        }
        
        protected void gvdetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            SqlConnection con1 = new SqlConnection(@"user id=sa;password=ssa;database=Mohan;data source=PCTH101\PCTH101");
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                con1.Open();
                var ddl = (DropDownList)e.Row.FindControl("DropDownList3");

                SqlCommand cmd = new SqlCommand("select productid, productname + ' - ' + price as new from productinfo1", con1);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                con1.Close();
                ddl.DataSource = ds;
                ddl.DataTextField = "new";
                //   ddl.DataTextField = "price";
                ddl.DataValueField = "productid";
                ddl.DataBind();
                ddl.Items.Insert(0, new ListItem("--Select--", "0"));
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                DropDownList DropDownList1 = (DropDownList)e.Row.FindControl("DropDownList1");
                SqlCommand cmd = new SqlCommand("select productid, productname + ' - ' + price as new1 from productinfo2", con1);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                con1.Close();
                DropDownList1.DataSource = ds;
                DropDownList1.DataTextField = "new1";
                //   ddl.DataTextField = "price";
                DropDownList1.DataValueField = "productid";
                DropDownList1.DataBind();
                DropDownList1.Items.Insert(0, new ListItem("--Select--", "0"));

            }
        }
       
        protected void gvdetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvdetail.PageIndex = e.NewPageIndex;
            BindGridview();
        }

        protected void gvdetail_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvdetail.EditIndex = -1;
            BindGridview();
        }

        protected void gvdetail_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("AddNew"))
            {
                DropDownList DropDownList1 = (DropDownList)gvdetail.FooterRow.FindControl("DropDownList1");
                TextBox txtname = (TextBox)gvdetail.FooterRow.FindControl("txtpname");
                TextBox txtprice = (TextBox)gvdetail.FooterRow.FindControl("txtprice");
                crudoperation("INSERT", txtname.Text, txtprice.Text, 0);
                SqlCommand cmd = new SqlCommand("insert into tbl_data (ddlvalue) values (@ddlvalue)", con);  
        cmd.Parameters.AddWithValue("ddlvalue", DropDownList1.SelectedItem.Text);  
        con.Open();  
        cmd.ExecuteNonQuery();
        con.Close();
            }
        }

        protected void gvdetail_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int productid = Convert.ToInt32(gvdetail.DataKeys[e.RowIndex].Values["productid"].ToString());
            string productname = gvdetail.DataKeys[e.RowIndex].Values["productname"].ToString();
            crudoperation("DELETE", productname, "", productid);
        
        }

        protected void gvdetail_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvdetail.EditIndex = e.NewEditIndex;
            BindGridview();

        }

        protected void gvdetail_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int productid = Convert.ToInt32(gvdetail.DataKeys[e.RowIndex].Values["productid"].ToString());
            TextBox txtname = (TextBox)gvdetail.Rows[e.RowIndex].FindControl("txtProductname");
            TextBox txtprice = (TextBox)gvdetail.Rows[e.RowIndex].FindControl("txtprice");
            crudoperation("UPDATE", txtname.Text, txtprice.Text, productid);
        
        }
        protected void crudoperation(string status, string productname, string price, int productid)
        {
            SqlConnection con = new SqlConnection(@"user id=sa;password=ssa;database=Mohan;data source=PCTH101\PCTH101");
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("crudoperation", con);
                cmd.CommandType = CommandType.StoredProcedure;
                if (status == "INSERT")
                {
                    cmd.Parameters.AddWithValue("@status", status);
                    cmd.Parameters.AddWithValue("@productname", productname);
                    cmd.Parameters.AddWithValue("@price", price);
                }
                else if (status == "UPDATE")
                {
                    cmd.Parameters.AddWithValue("@status", status);
                    cmd.Parameters.AddWithValue("@productname", productname);
                    cmd.Parameters.AddWithValue("@price", price);
                    cmd.Parameters.AddWithValue("@productid", productid);
                }
                else if (status == "DELETE")
                {
                    cmd.Parameters.AddWithValue("@status", status);
                    cmd.Parameters.AddWithValue("@productid", productid);
                }
                cmd.ExecuteNonQuery();
                lblresult.ForeColor = Color.Green;
                lblresult.Text = productname + " details " + status.ToLower() + "d successfully";
                gvdetail.EditIndex = -1;
                BindGridview();
                
            }
        }

        protected void gvdetail_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

      
      

         

    }
}

Karthik_Mahalingam

в чем проблема?

GrpSMK

в третьем столбце GridView будет на языке DDL.когда я выберите значение dropdownlist Для, что GridView в строке должен быть редактируемым, как?

0 Ответов