Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
XML
<head id="Head1" runat="server">
    <title>Create Docket</title>
    <script type = "text/javascript">

    function Check_Click(objRef) {
        //Get the Row based on checkbox
        var row = objRef.parentNode.parentNode;

        //Get the reference of GridView
        var GridView = row.parentNode;

        //Get all input elements in Gridview
        var inputList = GridView.getElementsByTagName("input");

        for (var i = 0; i < inputList.length; i++) {
            //The First element is the Header Checkbox
            var headerCheckBox = inputList[0];

            //Based on all or none checkboxes
            //are checked check/uncheck Header Checkbox
            var checked = true;
            if (inputList[i].type == "checkbox" && inputList[i] != headerCheckBox) {
                if (!inputList[i].checked) {
                    checked = false;
                    break;
                }
            }
        }
        headerCheckBox.checked = checked;

    }
    function checkAll(objRef) {
        var GridView = objRef.parentNode.parentNode.parentNode;
        var inputList = GridView.getElementsByTagName("input");
        for (var i = 0; i < inputList.length; i++) {
            var row = inputList[i].parentNode.parentNode;
            if (inputList[i].type == "checkbox" && objRef != inputList[i]) {
                if (objRef.checked) {
                    inputList[i].checked = true;
                }
                else {
                    if (row.rowIndex % 2 == 0) {
                        row.style.backgroundColor = "#C2D69B";
                    }
                    else {
                        row.style.backgroundColor = "white";
                    }
                    inputList[i].checked = false;
                }
            }
        }
    }

</script>
</head>
<body style="height: 451px; width: 860px">
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <div>
     <asp:GridView ID="gvAll" runat="server"
    AutoGenerateColumns = "false" Font-Names = "Arial"
    Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"
    HeaderStyle-BackColor = "green" AllowPaging ="true"
    OnPageIndexChanging = "OnPaging" PageSize = "10" Height="30px" Width="341px" >
   <Columns>
    <asp:TemplateField>
        <HeaderTemplate>
            <asp:CheckBox ID="chkAll" runat="server" onclick = "checkAll(this);"
            AutoPostBack = "true"  OnCheckedChanged = "CheckBox_CheckChanged"/>
        </HeaderTemplate>
        <ItemTemplate>
            <asp:CheckBox ID="chk" runat="server" onclick = "Check_Click(this)"
            AutoPostBack = "true"  OnCheckedChanged = "CheckBox_CheckChanged" />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField = "DocketNo" HeaderText = "DocketNo"
     HtmlEncode = "false" />
    <asp:BoundField DataField = "Date" HeaderText = "Date"
    HtmlEncode = "false" />
    <asp:BoundField DataField = "BranchCode" HeaderText = "BranchCode"
    HtmlEncode = "false" />
        <asp:BoundField DataField = "To" HeaderText = "To"
     HtmlEncode = "false" />
    <asp:BoundField DataField = "PKTS" HeaderText = "No of pkg"
    HtmlEncode = "false" />
    <asp:BoundField DataField = "ActWt" HeaderText = "ActWt"
    HtmlEncode = "false" />
         <asp:BoundField DataField = "ChargeWt" HeaderText = "ChargeW"
    HtmlEncode = "false" />
        <asp:BoundField DataField = "Description" HeaderText = "Description"
     HtmlEncode = "false" />
    <asp:BoundField DataField = "Remarks" HeaderText = "Remarks"
    HtmlEncode = "false" />
    <asp:BoundField DataField = "Mode" HeaderText = "Mode"
    HtmlEncode = "false" />
     <asp:BoundField DataField = "ChallanNo" HeaderText = "ChallanNo"
    HtmlEncode = "false" />
       <asp:BoundField DataField = "VehicleNo" HeaderText = "VehicleNo"
    HtmlEncode = "false" />
        <asp:BoundField DataField = "ChallanDate" HeaderText = "ChallanDate"
    HtmlEncode = "false" />
        <asp:BoundField DataField = "RChallanDate" HeaderText = "RChallanDate"
    HtmlEncode = "false" />
        <asp:BoundField DataField = "FromHub" HeaderText = "FromHub"
    HtmlEncode = "false" />
        <asp:BoundField DataField = "ToHub" HeaderText = "ToHub"
    HtmlEncode = "false" />
   </Columns>
   <AlternatingRowStyle BackColor="#C2D69B"  />
</asp:GridView>

            <br />
           <table border="1">
<tr>
    <td>
             <asp:Label ID="lblMode" Text="Mode" runat="server"></asp:Label>
             <br />
            <asp:DropDownList ID="dropdownMode" runat="server">
                <asp:ListItem>Air</asp:ListItem>
                <asp:ListItem>Surface</asp:ListItem>
                <asp:ListItem>Rail</asp:ListItem>
             </asp:DropDownList>
       </td>
         <td>
         <asp:Label ID="lblVehicleNo" Text="Vehicle No." runat="server"></asp:Label>
            <asp:TextBox ID="txtVehicleNo" runat="server"></asp:TextBox>
           </td>
        <td>
             <asp:Label ID ="lblChallanDate" Text="Challan Date" runat="server"></asp:Label>
             <asp:TextBox ID="txtChallanDate" runat="server"></asp:TextBox>
           </td>
        <td>
             <asp:Label ID="lblFromHub" Text="From Hub" runat="server"></asp:Label>
            <asp:TextBox ID="txtFromHub" runat="server"></asp:TextBox>
            </td>
        <td>
            <asp:Label ID="lblToHub" Text="To Hub" runat="server"></asp:Label>
            <asp:TextBox ID="txtToHub" runat="server"></asp:TextBox>
           </td>
               </tr>

        <tr>
            <td>
                <asp:Label ID="lblChallanNo" Text="Challan No." runat="server"></asp:Label>
                <asp:TextBox ID="txtChallanNo" runat="server"></asp:TextBox>
            </td>
            <td>
                <asp:Label ID="lblRchallanDate" Text="RChallan Date" runat="server"></asp:Label>
                <asp:TextBox ID="txtRChallanDate" runat="server"></asp:TextBox>
            </td>
            <td>
                <asp:Label ID="lblLHCNo" Text="LHCNo" runat="server"></asp:Label>
                <asp:TextBox ID="txtLHCNo" runat="server"></asp:TextBox>
            </td>
            <td>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Button ID="FindLHCNo" Text="Find" runat="server" OnClick="FindLHCNo_Click" />

                <asp:Label ID="Label1" runat="server"></asp:Label>

            </td>
            <td>
                <asp:Label ID="lblLHCDate" Text="LHC Date" runat="server"></asp:Label>
                <asp:TextBox ID="txtLHCDate" runat="server"></asp:TextBox>
            </td>

        </tr>
               </table>
             <br />
            <p align="center">
                <asp:Button ID="Update" runat="server" Text="Update Challan" OnClick="Update_Click" /> </p>
<asp:GridView ID="gvSelected" runat="server"
AutoGenerateColumns = "false" Font-Names = "Arial"
Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"
HeaderStyle-BackColor = "green" EmptyDataText = "No Records Selected"  >
<Columns>
     <asp:TemplateField HeaderText="DocketNo"
                        SortExpression="DocketNo">
                        <ItemTemplate>
                            <asp:TextBox ID="txtDocketNo" runat="server" Width="50px"
                                Text='<%# Bind("DocketNo") %>'
                                ReadOnly="true" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>

      <asp:TemplateField HeaderText="Date"
                        SortExpression="Date">
                        <ItemTemplate>
                            <asp:TextBox ID="txtDate" runat="server" Width="50px"
                                Text='<%# Bind("Date") %>'
                                ReadOnly="true" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>

       <asp:TemplateField HeaderText="BranchCode"
                        SortExpression="BranchCode">
                        <ItemTemplate>
                            <asp:TextBox ID="txtBranchCode" runat="server" Width="50px"
                                Text='<%# Bind("BranchCode") %>'
                                ReadOnly="true" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>

      <asp:TemplateField HeaderText="To"
                        SortExpression="To">
                        <ItemTemplate>
                            <asp:TextBox ID="txtTo" runat="server" Width="50px"
                                Text='<%# Bind("To") %>'
                                ReadOnly="true" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>

      <asp:TemplateField HeaderText="PKTS"
                        SortExpression="PKTS">
                        <ItemTemplate>
                            <asp:TextBox ID="txtPKTS" runat="server" Width="50px"
                                Text='<%# Bind("PKTS") %>'
                                ReadOnly="true" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>

      <asp:TemplateField HeaderText="ActWt"
                        SortExpression="ActWt">
                        <ItemTemplate>
                            <asp:TextBox ID="txtActWt" runat="server" Width="50px"
                                Text='<%# Bind("ActWt") %>'
                                ReadOnly="true" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>

      <asp:TemplateField HeaderText="ChargeWt"
                        SortExpression="ChargeWt">
                        <ItemTemplate>
                            <asp:TextBox ID="txtChargeWt" runat="server" Width="50px"
                                Text='<%# Bind("ChargeWt") %>'
                                ReadOnly="true" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>

<asp:TemplateField HeaderText="Description"
                        SortExpression="Description">
                        <ItemTemplate>
                            <asp:TextBox ID="txtDescription" runat="server" Width="50px"
                                Text='<%# Bind("Description") %>'
                                ReadOnly="true" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>

      <asp:TemplateField HeaderText="Remarks"
                        SortExpression="Remarks">
                        <ItemTemplate>
                            <asp:TextBox ID="txtRemarks" runat="server" Width="50px"
                                Text='<%# Bind("Remarks") %>'
                                ReadOnly="true" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>

      <asp:TemplateField HeaderText="Mode"
                        SortExpression="Mode">
                        <ItemTemplate>
                            <asp:TextBox ID="txtMode" runat="server" Width="50px"
                                Text='<%# Bind("Mode") %>'
                                ReadOnly="true" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>



      <asp:TemplateField HeaderText="ChallanNo"
                        SortExpression="ChallanNo">
                        <ItemTemplate>
                            <asp:TextBox ID="txtChallanNo" runat="server" Width="50px"
                                Text='<%# Bind("ChallanNo") %>'
                                ReadOnly="true" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>

      <asp:TemplateField HeaderText="VehicleNo"
                        SortExpression="VehicleNo">
                        <ItemTemplate>
                            <asp:TextBox ID="txtVehicleNo" runat="server" Width="50px"
                                Text='<%# Bind("VehicleNo") %>'
                                ReadOnly="true" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>

      <asp:TemplateField HeaderText="ChallanDate"
                        SortExpression="ChallanDate">
                        <ItemTemplate>
                            <asp:TextBox ID="txtChallanDate" runat="server" Width="50px"
                                Text='<%# Bind("ChallanDate") %>'
                                ReadOnly="true" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>

     <asp:TemplateField HeaderText="RChallanDate"
                        SortExpression="RChallanDate">
                        <ItemTemplate>
                            <asp:TextBox ID="txtRChallanDate" runat="server" Width="50px"
                                Text='<%# Bind("RChallanDate") %>'
                                ReadOnly="true" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>

     <asp:TemplateField HeaderText="FromHub"
                        SortExpression="FromHub">
                        <ItemTemplate>
                            <asp:TextBox ID="txtFromHub" runat="server" Width="50px"
                                Text='<%# Bind("FromHub") %>'
                                ReadOnly="true" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>

     <asp:TemplateField HeaderText="ToHub"
                        SortExpression="ToHub">
                        <ItemTemplate>
                            <asp:TextBox ID="txtToHub" runat="server" Width="50px"
                                Text='<%# Bind("ToHub") %>'
                                ReadOnly="true" ForeColor="Blue"
                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>

 </Columns>
</asp:GridView>

the C# Code for this is
C#
public partial class FNC : System.Web.UI.Page
{
      
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindPrimaryGrid();
            BindSecondaryGrid();
        }
    }
    private void BindPrimaryGrid()
    {
        string constr = ConfigurationManager.ConnectionStrings["amitpandeyConnectionString"].ConnectionString;
        string query = "select * from CreateDocket";
        SqlConnection con = new SqlConnection(constr);
        SqlDataAdapter sda = new SqlDataAdapter(query, con);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        gvAll.DataSource = dt;
        gvAll.DataBind();
    }

    protected void OnPaging(object sender, GridViewPageEventArgs e)
    {
        GetData();
        gvAll.PageIndex = e.NewPageIndex;
        BindPrimaryGrid();
        SetData();
    }

    private void GetData()
    {
        DataTable dt;
        if (ViewState["SelectedRecords"] != null)
            dt = (DataTable)ViewState["SelectedRecords"];
        else
            dt = CreateDataTable();
        CheckBox chkAll = (CheckBox)gvAll.HeaderRow
                            .Cells[0].FindControl("chkAll");
        for (int i = 0; i < gvAll.Rows.Count; i++)
        {
            if (chkAll.Checked)
            {
                dt = AddRow(gvAll.Rows[i], dt);
            }
            else
            {
                CheckBox chk = (CheckBox)gvAll.Rows[i]
                                .Cells[0].FindControl("chk");
                if (chk.Checked)
                {
                    dt = AddRow(gvAll.Rows[i], dt);
                }
                else
                {
                    dt = RemoveRow(gvAll.Rows[i], dt);
                }
            }
        }
        ViewState["SelectedRecords"] = dt;
    }

    private void SetData()
    {
        CheckBox chkAll = (CheckBox)gvAll.HeaderRow.Cells[0].FindControl("chkAll");
        chkAll.Checked = true;
        if (ViewState["SelectedRecords"] != null)
        {
            DataTable dt = (DataTable)ViewState["SelectedRecords"];
            for (int i = 0; i < gvAll.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)gvAll.Rows[i].Cells[0].FindControl("chk");
                if (chk != null)
                {
                    DataRow[] dr = dt.Select("DocketNo = '" + gvAll.Rows[i].Cells[1].Text + "'");
                    chk.Checked = dr.Length > 0;
                    if (!chk.Checked)
                    {
                        chkAll.Checked = false;
                    }
                }
            }
        }
    }

    private DataTable CreateDataTable()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("DocketNo");
        dt.Columns.Add("Date");
        dt.Columns.Add("BranchCode");
        dt.Columns.Add("To");
        dt.Columns.Add("PKTS");
        dt.Columns.Add("ActWt");
        dt.Columns.Add("ChargeWt");
        dt.Columns.Add("Description");
        dt.Columns.Add("Remarks");
        dt.Columns.Add("Mode");
        dt.Columns.Add("ChallanNo");
        dt.Columns.Add("VehicleNo");
        dt.Columns.Add("ChallanDate");
        dt.Columns.Add("RChallanDate");
        dt.Columns.Add("FromHub");
        dt.Columns.Add("ToHub");
        dt.AcceptChanges();
        return dt;
    }

    private DataTable AddRow(GridViewRow gvRow, DataTable dt)
    {
        DataRow[] dr = dt.Select("DocketNo = '" + gvRow.Cells[1].Text + "'");
        if (dr.Length <= 0)
        {
            dt.Rows.Add();
            dt.Rows[dt.Rows.Count - 1]["BranchCode"] = gvRow.Cells[3].Text;
            dt.Rows[dt.Rows.Count - 1]["Date"] = gvRow.Cells[2].Text;
            dt.Rows[dt.Rows.Count - 1]["DocketNo"] = gvRow.Cells[1].Text;
            dt.Rows[dt.Rows.Count - 1]["To"] = gvRow.Cells[4].Text;
            dt.Rows[dt.Rows.Count - 1]["PKTS"] = gvRow.Cells[5].Text;
            dt.Rows[dt.Rows.Count - 1]["ActWt"] = gvRow.Cells[6].Text;
            dt.Rows[dt.Rows.Count - 1]["ChargeWt"] = gvRow.Cells[7].Text;
            dt.Rows[dt.Rows.Count - 1]["Description"] = gvRow.Cells[8].Text;
            dt.Rows[dt.Rows.Count - 1]["Remarks"] = gvRow.Cells[9].Text;
            dt.Rows[dt.Rows.Count - 1]["Mode"] = gvRow.Cells[10].Text;
            dt.Rows[dt.Rows.Count - 1]["ChallanNo"] = gvRow.Cells[11].Text;
            dt.Rows[dt.Rows.Count - 1]["VehicleNo"] = gvRow.Cells[12].Text;
            dt.Rows[dt.Rows.Count - 1]["ChallanDate"] = gvRow.Cells[13].Text;
            dt.Rows[dt.Rows.Count - 1]["RChallanDate"] = gvRow.Cells[14].Text;
            dt.Rows[dt.Rows.Count - 1]["FromHub"] = gvRow.Cells[15].Text;
            dt.Rows[dt.Rows.Count - 1]["ToHub"] = gvRow.Cells[16].Text;
            dt.AcceptChanges();
        }
        return dt;
    }

    private DataTable RemoveRow(GridViewRow gvRow, DataTable dt)
    {
        DataRow[] dr = dt.Select("DocketNo = '" + gvRow.Cells[1].Text + "'");
        if (dr.Length > 0)
        {
            dt.Rows.Remove(dr[0]);
            dt.AcceptChanges();
        }
        return dt;
    }

    protected void CheckBox_CheckChanged(object sender, EventArgs e)
    {
        GetData();
        SetData();
        BindSecondaryGrid();
    }

    private void BindSecondaryGrid()
    {
        DataTable dt = (DataTable)ViewState["SelectedRecords"];
        gvSelected.DataSource = dt;
        gvSelected.DataBind();
    }
    protected void FindLHCNo_Click(object sender, EventArgs e)
    {

        string str = ConfigurationManager.ConnectionStrings["amitpandeyConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(str);
        SqlCommand com;
        con.Open();
        string query = "select * from LHC where LHCNo='" + txtLHCNo.Text.Trim() + "'"; 
        com = new SqlCommand(query, con);
        SqlDataReader reader = com.ExecuteReader();
        if (reader.Read())
        {
            txtLHCDate.Text = reader["Date"].ToString();
        }
        else
        {
            Label1.Text = "Wrong LHC No.";
        }
    }
    protected void Update_Click(object sender, EventArgs e)
    {

    }
}

What I want is second gridview shows me the selected records there are some textboxes which values I want to take them in the gridview i.e Dropdown Mode give me the mode I want to copy That dropdown value to all the columns mode set to that vgalue and the vehicle No text to all the columns equal to that textbox value . Is there any possible way.
Posted
Updated 12-Mar-15 20:13pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900