Set ASP.Net GridView row add limit based on DropDownList value using C# and VB.Net

Last Reply one month ago By pandeyism

Posted one month ago

I have dropdown in which two type NinePack and SixPack , i want to control row limit in gridview using dropdownlist list type. if user will access then show message.

i am inserting row into gridview from textboxes using viewstate.Now i want, when i select NinePack From dropdownlist then in gridview restrict user not to insert more then nine row record or greater then Nine Line same in sixpack when i select sixpack in dropdownlist then six row can be added from textbox.

Pleae guide here is html of gridview.

<div>
    Ref No.
    <asp:TextBox ID="txtRefno" CssClass="form-control input-lg" Placeholder="Ref No"
        runat="server" OnTextChanged="txtRefno_TextChanged" AutoPostBack="True"></asp:TextBox>
    &nbsp; Order No.
    <asp:TextBox ID="txtOrder" CssClass="form-control input-lg" Placeholder="Order No"
        runat="server" Height="16px" Width="68px" AutoPostBack="true" OnTextChanged="txtOrder_TextChanged"></asp:TextBox>
    <asp:Label ID="lbmsg" runat="server" Text="." Font-Bold="True" Font-Size="Large"
        ForeColor="Red"></asp:Label>
    &nbsp;&nbsp;
    <asp:TextBox ID="txtpackno" CssClass="form-control input-lg" Placeholder="Order No"
        AutoPostBack="true" Visible="false" runat="server" Height="16px" Width="68px"
        OnTextChanged="txtpackno_TextChanged"></asp:TextBox>
    Order Qty :
    <asp:Label ID="lborderqty" runat="server" Text="." Font-Bold="True" Font-Size="X-Large"></asp:Label>
    Produce Qty :
    <asp:Label ID="lbprdqty" runat="server" Text="." Font-Bold="True" Font-Size="X-Large"></asp:Label>
    Packs Qty :
    <asp:Label ID="lbpkqty" runat="server" Text="." Font-Bold="True" Font-Size="X-Large"></asp:Label>
    Pending Qty :
    <asp:Label ID="lbpendingqty" runat="server" Text="." Font-Bold="True" Font-Size="X-Large"></asp:Label>
    <br />
    Packs Type :
    <asp:DropDownList ID="DDLPktype" runat="server">
        <asp:ListItem>NinePack</asp:ListItem>
        <asp:ListItem>SixPack</asp:ListItem>
    </asp:DropDownList>
    <br />
    <br />
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    <asp:TextBox ID="TextBox1" AutoPostBack="true" runat="server" Placeholder="Input Bale No"
        OnTextChanged="TextBox1_TextChanged" Height="16px" Width="100px" Style="margin-left: 0px"></asp:TextBox>
    <asp:DropDownList ID="DropDownList1" runat="server" Enabled="false" Height="20px"
        Width="130px">
    </asp:DropDownList>
    <asp:TextBox ID="txtorderno" Placeholder="Order No" ReadOnly="true" runat="server"
        Height="16px" Width="51px"></asp:TextBox>
    <asp:TextBox ID="txtqty" Placeholder="QTY" ReadOnly="true" runat="server" Height="16px"
        Width="56px"></asp:TextBox>
    &nbsp;
    <asp:Button ID="Button1" runat="server" Text="Add" Visible="false" OnClick="Button1_Click"
        Height="26px" Width="35px" />
    <asp:Label ID="Label1" runat="server" Text="." ForeColor="#CC0000"></asp:Label>
    <asp:Label ID="lbgvck" runat="server" Text="." ForeColor="#CC0000"></asp:Label>
    <asp:Label ID="lbpk" runat="server" Text="."></asp:Label>
    &nbsp;
</div>
<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="#66ccff" HeaderStyle-ForeColor="White"
    CssClass="mygrdContent" PagerStyle-CssClass="pager" HeaderStyle-CssClass="header"
    RowStyle-CssClass="rows" AutoGenerateColumns="false" OnRowDeleting="GridView1_RowDeleting"
    OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" AllowSorting="True"
    OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Bale_No">
            <ItemTemplate>
                <asp:Label ID="Prdno" runat="server" Text='<%#Bind("Prdno")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Description">
            <ItemTemplate>
                <asp:Label ID="Descriptionitem" runat="server" Text='<%#Bind("Descriptionitem")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Orderno">
            <ItemTemplate>
                <asp:Label ID="Orderno" runat="server" Text='<%#Bind("Orderno")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="QTY">
            <ItemTemplate>
                <asp:Label ID="QTY" runat="server" Text='<%#Bind("QTY")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
    </Columns>
    <HeaderStyle BackColor="#66CCFF" ForeColor="White"></HeaderStyle>
    <SelectedRowStyle BackColor="Yellow" />
</asp:GridView>

 

 

You are viewing reply posted by: pandeyism one month ago.
Posted one month ago

Hi akhter,

Refer below sample.

HTML

Qty :
<asp:TextBox runat="server" ID="txtqty" />
Description :
<asp:DropDownList runat="server" ID="ddlItems">
</asp:DropDownList>
<asp:DropDownList ID="DDLPktype" runat="server">
    <asp:ListItem Text="NinePack" Value="9" />
    <asp:ListItem Text="SixPack" Value="6" />
</asp:DropDownList>
<asp:Button ID="Button1" Text="Insert" runat="server" OnClick="Insert" />
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false" OnRowEditing="GridView1_RowEditing">
    <Columns>
        <asp:BoundField DataField="Descriptionitem" HeaderText="Descriptionitem" />
        <asp:BoundField DataField="QTY" HeaderText="QTY" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lnkEdit" Text="Edit" runat="server" CommandName="Edit" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:LinkButton ID="lnkUpdate" Text="Update" runat="server" OnClick="OnUpdate" />
                <asp:LinkButton ID="lnkCancel" Text="Cancel" runat="server" OnClick="OnCancel" />
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

DataTable dt = new DataTable();
DataRow dr;
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        dt.Columns.Add("Descriptionitem");
        dt.Columns.Add("Qty");
        DataTable dtddl = new DataTable();
        dtddl.Columns.Add("Descriptionitem");
        dtddl.Columns.Add("Codeitem");
        dtddl.Rows.Add("A", 1);
        dtddl.Rows.Add("B", 2);
        dtddl.Rows.Add("C", 3);
        ddlItems.DataSource = dtddl;
        ddlItems.DataTextField = "Descriptionitem";
        ddlItems.DataValueField = "Codeitem";
        ddlItems.DataBind();
        ddlItems.Items.Insert(0, new ListItem("Select", "0"));
        ViewState["dt"] = dt;
    }
}

protected void BindGrid()
{
    GridView1.DataSource = ViewState["dt"] as DataTable;
    GridView1.DataBind();
}
protected void Insert(object sender, EventArgs e)
{
    dt = ViewState["dt"] as DataTable;
    dr = dt.NewRow();
    dr["Descriptionitem"] = ddlItems.SelectedItem.Text;
    dr["QTY"] = txtqty.Text;
    if (dt.Rows.Count < Convert.ToInt16(DDLPktype.SelectedItem.Value))
    {
        dt.Rows.Add(dr);
    }
    else
    {
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('You can not insert more than " + dt.Rows.Count + " rows')", true);
    }
    GridView1.DataSource = dt;
    GridView1.DataBind();
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    BindGrid();
}

protected void OnUpdate(object sender, EventArgs e)
{
    GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
    string descriptionitem = (row.Cells[0].Controls[0] as TextBox).Text;
    int qty = Convert.ToInt32(((row.Cells[1].Controls[0] as TextBox).Text));
    DataTable dt = ViewState["dt"] as DataTable;
    dt.Rows[row.RowIndex]["Descriptionitem"] = descriptionitem;
    dt.Rows[row.RowIndex]["QTY"] = qty;
    ViewState["dt"] = dt;
    GridView1.EditIndex = -1;
    BindGrid();
}

protected void OnCancel(object sender, EventArgs e)
{
    GridView1.EditIndex = -1;
    BindGrid();
}

VB.Net

Private dt As DataTable = New DataTable()
Private dr As DataRow
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        dt.Columns.Add("Descriptionitem")
        dt.Columns.Add("Qty")
        Dim dtddl As DataTable = New DataTable()
        dtddl.Columns.Add("Descriptionitem")
        dtddl.Columns.Add("Codeitem")
        dtddl.Rows.Add("A", 1)
        dtddl.Rows.Add("B", 2)
        dtddl.Rows.Add("C", 3)
        ddlItems.DataSource = dtddl
        ddlItems.DataTextField = "Descriptionitem"
        ddlItems.DataValueField = "Codeitem"
        ddlItems.DataBind()
        ddlItems.Items.Insert(0, New ListItem("Select", "0"))
        ViewState("dt") = dt
    End If
End Sub

Protected Sub BindGrid()
    GridView1.DataSource = TryCast(ViewState("dt"), DataTable)
    GridView1.DataBind()
End Sub

Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
    dt = TryCast(ViewState("dt"), DataTable)
    dr = dt.NewRow()
    dr("Descriptionitem") = ddlItems.SelectedItem.Text
    dr("QTY") = txtqty.Text
    If dt.Rows.Count < Convert.ToInt16(DDLPktype.SelectedItem.Value) Then
        dt.Rows.Add(dr)
    Else
        ClientScript.RegisterClientScriptBlock(Me.[GetType](), "", "alert('You can not insert more than " & dt.Rows.Count & " rows')", True)
    End If
    GridView1.DataSource = dt
    GridView1.DataBind()
End Sub

Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
    GridView1.EditIndex = e.NewEditIndex
    BindGrid()
End Sub

Protected Sub OnUpdate(ByVal sender As Object, ByVal e As EventArgs)
    Dim row As GridViewRow = TryCast((TryCast(sender, LinkButton)).NamingContainer, GridViewRow)
    Dim descriptionitem As String = (TryCast(row.Cells(0).Controls(0), TextBox)).Text
    Dim qty As Integer = Convert.ToInt32(((TryCast(row.Cells(1).Controls(0), TextBox)).Text))
    Dim dt As DataTable = TryCast(ViewState("dt"), DataTable)
    dt.Rows(row.RowIndex)("Descriptionitem") = descriptionitem
    dt.Rows(row.RowIndex)("QTY") = qty
    ViewState("dt") = dt
    GridView1.EditIndex = -1
    BindGrid()
End Sub

Protected Sub OnCancel(ByVal sender As Object, ByVal e As EventArgs)
    GridView1.EditIndex = -1
    BindGrid()
End Sub

Screenshot