locked
Move to particular row based on given input RRS feed

  • Question

  • User-807418713 posted

    Hello

    This Is My Aspx Page

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
    
    <!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>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:Label id="Label30" runat="server" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="10pt" ForeColor="Red" Text="MOVE TO PARTICULAR ROW" Width="210px"></asp:Label>
            <asp:TextBox id="TextBox7" runat="server" Width="45px"></asp:TextBox>
            <br />
            <asp:Label id="Label22" runat="server" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="10pt" ForeColor="MediumBlue" Text="SELECT ISSUES" Width="153px"></asp:Label>
            <asp:DropDownList id="DrpIssues" runat="server" ForeColor="Black" Width="115px" CssClass="chzn-select">
                        <asp:ListItem Selected="True">1 Issues</asp:ListItem>
                        <asp:ListItem>2 Issues</asp:ListItem>
                        <asp:ListItem>3 Issues</asp:ListItem>
                        <asp:ListItem>4 Issues</asp:ListItem>
                        <asp:ListItem>5 Issues</asp:ListItem>
                        <asp:ListItem>6 Issues</asp:ListItem>
                        <asp:ListItem>7 Issues</asp:ListItem>
                        <asp:ListItem>8 Issues</asp:ListItem>
                        <asp:ListItem>9 Issues</asp:ListItem>
                        <asp:ListItem>10 Issues</asp:ListItem>
                        <asp:ListItem>11 Issues</asp:ListItem>
                        <asp:ListItem>12 Issues</asp:ListItem>
                        <asp:ListItem>13 Issues</asp:ListItem>
                        <asp:ListItem>14 Issues</asp:ListItem>
                        <asp:ListItem>15 Issues</asp:ListItem>
                    </asp:DropDownList>
            <asp:Label id="Label29" runat="server" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="10pt" ForeColor="MediumBlue" Text="RATE" Width="63px"></asp:Label>
            <asp:TextBox id="RATE" runat="server" Font-Bold="True" Font-Size="11pt" ForeColor="#000000" Width="79px" AutoComplete="off"></asp:TextBox>
            <asp:Button ID="Add" runat="server" AccessKey="A" BackColor="Transparent" BorderColor="Transparent"
                BorderStyle="Dashed" BorderWidth="2px" Font-Bold="True" Font-Names="Palatino Linotype"
                Font-Size="12pt" ForeColor="#FFFFFF" Height="41px" OnClick="Add_Click" onmouseover="ValueCheck();"
                Style="background-image: url(images/btn_5.png); background-color: transparent"
                Text="Add " ValidationGroup="gt" Width="96px" /><br />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellSpacing="6"
                EmptyDataText="No Item Found !" Font-Bold="True" Font-Names="Palatino Linotype"
                Font-Size="12pt" OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="GridView1_RowDeleting" Style="text-align: center"
                Width="582px">
                <Columns>
                    <asp:TemplateField HeaderText="SI NO">
                        <ItemTemplate>
                            <%# Container.DataItemIndex + 1 %>
                        </ItemTemplate>
                        <HeaderStyle ForeColor="Red" HorizontalAlign="Left" Width="5%" />
                        <ItemStyle HorizontalAlign="Center" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="ISSUE NO">
                        <ItemTemplate>
                            <asp:Label ID="L1" runat="server" Text='<%#Eval("IN") %>'></asp:Label>
                        </ItemTemplate>
                        <HeaderStyle ForeColor="Red" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="RATE">
                        <ItemTemplate>
                            <asp:Label ID="L2" runat="server" Text='<%#Eval("RA") %>'></asp:Label>
                        </ItemTemplate>
                        <HeaderStyle ForeColor="Red" />
                    </asp:TemplateField>
                    <asp:CommandField ShowDeleteButton="True">
                        <HeaderStyle ForeColor="Red" />
                        <ItemStyle ForeColor="Red" />
                    </asp:CommandField>
                </Columns>
            </asp:GridView>
        </div>
        </form>
    </body>
    </html>
    

    This Is My Code Behind 

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    
    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
    
            }
        }
    
    
        private void BindGridView()
        {
            //Declare a datatable for the gridview
            DataTable dt = new DataTable();
            //Add Columns to the datatable
            
            dt.Columns.Add("IN");
            dt.Columns.Add("RA");
             
    
            //Define a datarow for the datatable dt
            DataRow dr = dt.NewRow();
    
            //Now add the datarow to the datatable
            dt.Rows.Add(dr);
            //Now bind the datatable to gridview
            GridView1.DataSource = dt;
            GridView1.DataBind();
            //Now hide the extra row of the grid view
            GridView1.Rows[0].Visible = false;
            //Delete row 0 from the datatable
            dt.Rows[0].Delete();
            dt.AcceptChanges();
            //View the datatable to the viewstate
            ViewState["RAWI"] = dt;
        }
        protected void Add_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
    
            dt = (DataTable)ViewState["RAWI"];
            DataRow dr = dt.NewRow();
            
            dr["IN"] = DrpIssues.Text;
            dr["RA"] = RATE.Text;
            
    
                //Add the datarow to the datatable
                dt.Rows.Add(dr);
                //Now bind the datatable to the gridview
                GridView1.DataSource = dt;
                GridView1.DataBind();
            
    
            ViewState["RAWI"] = dt;
    
    
            GridView1.Visible = true;
    
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
    
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            ((DataTable)ViewState["RAWI"]).Rows[e.RowIndex].Delete();
            ((DataTable)ViewState["RAWI"]).AcceptChanges();
    
            if (((DataTable)ViewState["RAWI"]).Rows.Count > 0)
            {
    
                GridView1.DataSource = (DataTable)ViewState["RAWI"];
                GridView1.DataBind();
            }
            else
            {
                BindGridView();
    
                GridView1.Visible = false;
    
            }
    
        }
    }
    

    Now see below screen im entering number 4 in drodpwinlist i selected 2issues rate 5

    now on add i want to show that in 4 th row how to do so 

    Thursday, January 14, 2021 10:19 AM

Answers

  • User-1330468790 posted

    Hi Gopi.MCA,

     

    You could use DataTable.Rows.InsertAt(DataRow, Index) method which will add the data row to the special indexed row.

    Reference:

    https://docs.microsoft.com/en-us/dotnet/api/system.data.datarowcollection.insertat?redirectedfrom=MSDN&view=net-5.0#System_Data_DataRowCollection_InsertAt_System_Data_DataRow_System_Int32_

     

    Regarding the codes, you could simply modify the Add_Click method from code behind:

     protected void Add_Click(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
    
                dt = (DataTable)ViewState["RAWI"];
                DataRow dr = dt.NewRow();
    
                dr["IN"] = DrpIssues.Text;
                dr["RA"] = RATE.Text;
    
                if (string.IsNullOrEmpty(TextBox7.Text))
                {
                    //Add the datarow to the datatable
                    dt.Rows.Add(dr);
    
                }
                else
                {
    
                    // You should gurantee that the input is an integer and define your logic if the input is not
                    // 0-base
                    int rowIndex = int.Parse(TextBox7.Text) - 1;
    
                    // Check to see if the row Index is out of bound
                    if (rowIndex > dt.Rows.Count || rowIndex < 0)
                    {
                        return;
                    }
    
                    dt.Rows.InsertAt(dr, rowIndex);
    
                }
    
    
                //Now bind the datatable to the gridview
                GridView1.DataSource = dt;
                GridView1.DataBind();
    
                ViewState["RAWI"] = dt;
    
                GridView1.Visible = true;
    
            }

    Demo:

     

    Hope helps.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 15, 2021 10:50 AM