locked
How to set checked row on retrieving data from database into gridview? RRS feed

  • Question

  • User-367318540 posted

    i want to set default checked row in gridview on retrieving data from database below is my html.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PackTrans.aspx.cs" Inherits="WebApplication1.PackTrans" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" />
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $(".js-example-placeholder-single").select2({
                    placeholder: "Select",
                    allowClear: true
                });
            });
        </script>
        <script type = "text/javascript">
    
    function Check_Click(objRef)
    
    {
    
        //Get the Row based on checkbox
    
        var row = objRef.parentNode.parentNode;
    
        if(objRef.checked)
    
        {
    
            //If checked change color to Aqua
    
            row.style.backgroundColor = "aqua";
    
        }
    
        else
    
        {   
    
            //If not checked change back to original color
    
            if(row.rowIndex % 2 == 0)
    
            {
    
               //Alternating Row Color
    
               row.style.backgroundColor = "#C2D69B";
    
            }
    
            else
    
            {
    
               row.style.backgroundColor = "white";
    
            }
    
        }
    
       
    
        //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;
    
       
    
    }
    
    </script>
        <script type = "text/javascript">
    
    function checkAll(objRef)
    
    {
    
        var GridView = objRef.parentNode.parentNode.parentNode;
    
        var inputList = GridView.getElementsByTagName("input");
    
        for (var i=0;i<inputList.length;i++)
    
        {
    
            //Get the Cell To find out ColumnIndex
    
            var row = inputList[i].parentNode.parentNode;
    
            if(inputList[i].type == "checkbox"  && objRef != inputList[i])
    
            {
    
                if (objRef.checked)
    
                {
    
                    //If the header checkbox is checked
    
                    //check all checkboxes
    
                    //and highlight all rows
    
                    row.style.backgroundColor = "aqua";
    
                    inputList[i].checked=true;
    
                }
    
                else
    
                {
    
                    //If the header checkbox is checked
    
                    //uncheck all checkboxes
    
                    //and change rowcolor back to original
    
                    if(row.rowIndex % 2 == 0)
    
                    {
    
                       //Alternating Row Color
    
                       row.style.backgroundColor = "#C2D69B";
    
                    }
    
                    else
    
                    {
    
                       row.style.backgroundColor = "white";
    
                    }
    
                    inputList[i].checked=false;
    
                }
    
            }
    
        }
    
    }
    
    </script> 
         <style type="text/css">
            .auto-style1 {
                width: 100%;
            }
            .auto-style2 {
                 height: 23px;
                 width: 330px;
             }
            .js-example-placeholder-single {}
             .auto-style3 {
                 width: 1321px;
             }
             .auto-style4 {
                 height: 271px;
                 width: 730px;
                 overflow: auto;
             }
        </style>
    
    <head runat="server">
       <title></title>
    </head>
    <body>
    
        <form id="form1" runat="server">
          <div>
    
            <div>
        
            <table class="auto-style1">
                <tr>
                    <td class="auto-style2">&nbsp;</td>
                    <td class="auto-style3">
                        <h1>&nbsp;Produce Bale Item Wise</h1>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style2">&nbsp;</td>
                    <td class="auto-style3">
                        <h4>&nbsp;&nbsp;Pack No : <asp:TextBox ID="txtPack" CssClass="form-control input-lg"   Placeholder="Pack No" AutoPostBack="true"  runat="server"  Width="67px" style="height: 25px" OnTextChanged="txtPack_TextChanged" ></asp:TextBox>
            &nbsp;
                                                        <asp:Button ID="btnUpdate" runat="server" Text="Transfer" OnClick = "Update" />
    
    
                    
                             Transfer To </h4>
      
         
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style2">&nbsp;</td>
                    <td class="auto-style3">
                        <div class="auto-style4">
    
    
                        <asp:GridView ID="GVallbb"  runat="server"  BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" AutoGenerateColumns = "false" Font-Names = "Arial" CssClass="HeaderFreez" ShowHeader = "true"
    
        Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"  class="grid_scroll" PageSize="5" ShowHeaderWhenEmpty="True" DataKeyNames = "Prdno" ShowFooter="True"  style="margin-right: 0px">
                            <AlternatingRowStyle BackColor="#F7F7F7" />
                            <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                            <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
                            <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                            <SortedAscendingCellStyle BackColor="#F4F4FD" />
                            <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
                            <SortedDescendingCellStyle BackColor="#D8D8F0" />
                            <SortedDescendingHeaderStyle BackColor="#3E3277" />
                            <Columns>
    
                <asp:TemplateField>
    
        <HeaderTemplate>
    
          <asp:CheckBox ID="checkAll" runat="server"    onclick = "checkAll(this);true" />
    
        </HeaderTemplate>
    
       <ItemTemplate>
    
         <asp:CheckBox ID="CheckBox1" runat="server" Enabled="false"  onclick = "Check_Click(this)" />
    
       </ItemTemplate>
    
    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Bale No">
                                    <ItemTemplate>
                                        <asp:HiddenField ID="hfBID" runat="server" Value='<%#Eval("Prdno")%>' />
                                        <asp:Label ID="Prdno" runat="server" Text='<%#Bind("Prdno")%>' ></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="Item Name">
                                    <ItemTemplate>
                                        <asp:Label ID="itemName" runat="server" Text='<%#Bind("Descriptionitem")%>'></asp:Label>
                                    </ItemTemplate>
                                     <FooterTemplate><asp:Label ID="lblTotalText" Text="Total" runat="server"></asp:Label></FooterTemplate>
                                </asp:TemplateField>
                                 <asp:TemplateField HeaderText="QTY">
                                    <ItemTemplate>
                                        <asp:Label ID="QTY1" runat="server" Text='<%#Bind("QTY1")%>'></asp:Label>
                                    </ItemTemplate>
                                     <FooterTemplate><asp:Label ID="lblTotal" runat="server"></asp:Label></FooterTemplate>
                                      <%--<FooterTemplate><asp:Label ID="lblTotalText" Text="Total" runat="server"></asp:Label></FooterTemplate>--%>
                                </asp:TemplateField>
                       
    
    
    
                               
                              
    
    
                     
                            </Columns>
                        </asp:GridView>
                            </div>
    
    
                    </td>
                    <td>&nbsp;</td>
                </tr>
            </table>
        
        </div>
        </div>
    
    
                    
        </form>
    </body>
    </html>
    

    Sunday, January 5, 2020 1:28 PM

Answers

  • User1535942433 posted

    Hi akhterr,

    Accroding to your description and codes,I couldn't understand your requriment clearly.Do you want to retrieve database based on a field and set default checked row?Or you need to get data which bind with checkbox from database,then set default checked row.

    Could you post more details about your requirment?

    I compare the textbox value with the gridview Prdno column value.If these are same,the gridview will be set checked row.I creat a little test for you.

    More details,you could refer to below codes:

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" />
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $(".js-example-placeholder-single").select2({
                    placeholder: "Select",
                    allowClear: true
                });
            });
        </script>
        <script type="text/javascript">
            function Check_Click(objRef) {
                //Get the Row based on checkbox
                var row = objRef.parentNode.parentNode;
                if (objRef.checked) {
                    //If checked change color to Aqua
                    row.style.backgroundColor = "aqua";
                }
                else {
                    //If not checked change back to original color
                    if (row.rowIndex % 2 == 0) {
                        //Alternating Row Color
                        row.style.backgroundColor = "#C2D69B";
                    }
                    else {
                        row.style.backgroundColor = "white";
                    }
                }
                //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;
            }
        </script>
        <script type="text/javascript">
            function checkAll(objRef) {
                var GridView = objRef.parentNode.parentNode.parentNode;
                var inputList = GridView.getElementsByTagName("input");     
                for (var i = 0; i < inputList.length; i++) {
                    //Get the Cell To find out ColumnIndex
                    var row = inputList[i].parentNode.parentNode;
                    if (inputList[i].type == "checkbox" && objRef != inputList[i]) {
                        if (objRef.checked) {
                            //If the header checkbox is checked
                            //check all checkboxes
                            //and highlight all rows
                            row.style.backgroundColor = "aqua";
                            inputList[i].checked = true;
                        }
                        else {
                            //If the header checkbox is checked
                            //uncheck all checkboxes
                            //and change rowcolor back to original
                            if (row.rowIndex % 2 == 0) {
                                //Alternating Row Color
                                row.style.backgroundColor = "#C2D69B";
                            }
                            else {
                                row.style.backgroundColor = "white";
                            }
                            inputList[i].checked = false;
                        }
                    }
                }
            }
        </script>
        <style type="text/css">
            .auto-style1 {
                width: 100%;
            }
    
            .auto-style2 {
                height: 23px;
                width: 330px;
            }
    
            .js-example-placeholder-single {
            }
    
            .auto-style3 {
                width: 1321px;
            }
    
            .auto-style4 {
                height: 271px;
                width: 730px;
                overflow: auto;
            }
        </style>

    <div>
    <div>
    <table class="auto-style1">
    <tr>
    <td class="auto-style2">&nbsp;</td>
    <td class="auto-style3">
    <h1>&nbsp;Produce Bale Item Wise</h1>
    </td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td class="auto-style2">&nbsp;</td>
    <td class="auto-style3">
    <h4>&nbsp;&nbsp;Pack No :
    <asp:TextBox ID="txtPack" CssClass="form-control input-lg" Placeholder="Pack No" AutoPostBack="true" runat="server" Width="67px" Style="height: 25px" OnTextChanged="txtPack_TextChanged"></asp:TextBox>
    &nbsp;
    <asp:Button ID="btnUpdate" runat="server" Text="Transfer"/>
    Transfer To </h4>
    </td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td class="auto-style2">&nbsp;</td>
    <td class="auto-style3">
    <div class="auto-style4">
    <asp:GridView ID="GVallbb" runat="server" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" AutoGenerateColumns="false" Font-Names="Arial" CssClass="HeaderFreez" ShowHeader="true"
    Font-Size="11pt" AlternatingRowStyle-BackColor="#C2D69B" class="grid_scroll" PageSize="5" ShowHeaderWhenEmpty="True" DataKeyNames="Prdno" ShowFooter="True" Style="margin-right: 0px">
    <AlternatingRowStyle BackColor="#F7F7F7" />
    <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
    <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
    <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
    <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
    <SortedAscendingCellStyle BackColor="#F4F4FD" />
    <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
    <SortedDescendingCellStyle BackColor="#D8D8F0" />
    <SortedDescendingHeaderStyle BackColor="#3E3277" />
    <Columns>
    <asp:TemplateField>
    <HeaderTemplate>
    <asp:CheckBox ID="checkAll" runat="server" onclick="checkAll(this);true" />
    </HeaderTemplate>
    <ItemTemplate>
    <asp:CheckBox ID="CheckBox1" runat="server" onclick="Check_Click(this)" />
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Bale No">
    <ItemTemplate>
    <asp:HiddenField ID="hfBID" runat="server" Value='<%#Eval("Prdno")%>' />
    <asp:Label ID="Prdno" runat="server" Text='<%#Bind("Prdno")%>'></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="Item Name">
    <ItemTemplate>
    <asp:Label ID="itemName" runat="server" Text='<%#Bind("Descriptionitem")%>'></asp:Label>
    </ItemTemplate>
    <FooterTemplate>
    <asp:Label ID="lblTotalText" Text="Total" runat="server"></asp:Label>
    </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="QTY">
    <ItemTemplate>
    <asp:Label ID="QTY1" runat="server" Text='<%#Bind("QTY1")%>'></asp:Label>
    </ItemTemplate>
    <FooterTemplate>
    <asp:Label ID="lblTotal" runat="server"></asp:Label>
    </FooterTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
    </div>
    </td>
    <td>&nbsp;</td>
    </tr>
    </table>
    </div>
    </div>

    Code-Behind:

    protected void bind()
            {
                string str, strSql;
                str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542ConnectionString"].ConnectionString;
                SqlConnection conn = new SqlConnection(str);
                strSql = "select * from Test2";
                SqlDataAdapter da = new SqlDataAdapter(strSql, str);
                DataSet ds = new DataSet();
                da.Fill(ds, "Test2");
                GVallbb.DataSource = ds.Tables[0].DefaultView;
                GVallbb.DataBind();
                conn.Close();
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bind();
                }
    
            }
    
            protected void txtPack_TextChanged(object sender, EventArgs e)
            {
                string value = txtPack.Text;
                for (int i = 0; i < GVallbb.Rows.Count; i++)
                {
                    string cellvalue = ((Label)GVallbb.Rows[i].FindControl("Prdno")).Text.ToString();
                    if (cellvalue == value)
                    {
                        CheckBox cb = (CheckBox)GVallbb.Rows[i].FindControl("CheckBox1");
                        cb.Checked = true;
                        GVallbb.Rows[i].BackColor= Color.Green;
                    }
                }
            }

    Result:

    Best regards,

    Yijing sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 6, 2020 3:11 AM

All replies

  • User1535942433 posted

    Hi akhterr,

    Accroding to your description and codes,I couldn't understand your requriment clearly.Do you want to retrieve database based on a field and set default checked row?Or you need to get data which bind with checkbox from database,then set default checked row.

    Could you post more details about your requirment?

    I compare the textbox value with the gridview Prdno column value.If these are same,the gridview will be set checked row.I creat a little test for you.

    More details,you could refer to below codes:

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" />
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $(".js-example-placeholder-single").select2({
                    placeholder: "Select",
                    allowClear: true
                });
            });
        </script>
        <script type="text/javascript">
            function Check_Click(objRef) {
                //Get the Row based on checkbox
                var row = objRef.parentNode.parentNode;
                if (objRef.checked) {
                    //If checked change color to Aqua
                    row.style.backgroundColor = "aqua";
                }
                else {
                    //If not checked change back to original color
                    if (row.rowIndex % 2 == 0) {
                        //Alternating Row Color
                        row.style.backgroundColor = "#C2D69B";
                    }
                    else {
                        row.style.backgroundColor = "white";
                    }
                }
                //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;
            }
        </script>
        <script type="text/javascript">
            function checkAll(objRef) {
                var GridView = objRef.parentNode.parentNode.parentNode;
                var inputList = GridView.getElementsByTagName("input");     
                for (var i = 0; i < inputList.length; i++) {
                    //Get the Cell To find out ColumnIndex
                    var row = inputList[i].parentNode.parentNode;
                    if (inputList[i].type == "checkbox" && objRef != inputList[i]) {
                        if (objRef.checked) {
                            //If the header checkbox is checked
                            //check all checkboxes
                            //and highlight all rows
                            row.style.backgroundColor = "aqua";
                            inputList[i].checked = true;
                        }
                        else {
                            //If the header checkbox is checked
                            //uncheck all checkboxes
                            //and change rowcolor back to original
                            if (row.rowIndex % 2 == 0) {
                                //Alternating Row Color
                                row.style.backgroundColor = "#C2D69B";
                            }
                            else {
                                row.style.backgroundColor = "white";
                            }
                            inputList[i].checked = false;
                        }
                    }
                }
            }
        </script>
        <style type="text/css">
            .auto-style1 {
                width: 100%;
            }
    
            .auto-style2 {
                height: 23px;
                width: 330px;
            }
    
            .js-example-placeholder-single {
            }
    
            .auto-style3 {
                width: 1321px;
            }
    
            .auto-style4 {
                height: 271px;
                width: 730px;
                overflow: auto;
            }
        </style>

    <div>
    <div>
    <table class="auto-style1">
    <tr>
    <td class="auto-style2">&nbsp;</td>
    <td class="auto-style3">
    <h1>&nbsp;Produce Bale Item Wise</h1>
    </td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td class="auto-style2">&nbsp;</td>
    <td class="auto-style3">
    <h4>&nbsp;&nbsp;Pack No :
    <asp:TextBox ID="txtPack" CssClass="form-control input-lg" Placeholder="Pack No" AutoPostBack="true" runat="server" Width="67px" Style="height: 25px" OnTextChanged="txtPack_TextChanged"></asp:TextBox>
    &nbsp;
    <asp:Button ID="btnUpdate" runat="server" Text="Transfer"/>
    Transfer To </h4>
    </td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td class="auto-style2">&nbsp;</td>
    <td class="auto-style3">
    <div class="auto-style4">
    <asp:GridView ID="GVallbb" runat="server" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" AutoGenerateColumns="false" Font-Names="Arial" CssClass="HeaderFreez" ShowHeader="true"
    Font-Size="11pt" AlternatingRowStyle-BackColor="#C2D69B" class="grid_scroll" PageSize="5" ShowHeaderWhenEmpty="True" DataKeyNames="Prdno" ShowFooter="True" Style="margin-right: 0px">
    <AlternatingRowStyle BackColor="#F7F7F7" />
    <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
    <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
    <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
    <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
    <SortedAscendingCellStyle BackColor="#F4F4FD" />
    <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
    <SortedDescendingCellStyle BackColor="#D8D8F0" />
    <SortedDescendingHeaderStyle BackColor="#3E3277" />
    <Columns>
    <asp:TemplateField>
    <HeaderTemplate>
    <asp:CheckBox ID="checkAll" runat="server" onclick="checkAll(this);true" />
    </HeaderTemplate>
    <ItemTemplate>
    <asp:CheckBox ID="CheckBox1" runat="server" onclick="Check_Click(this)" />
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Bale No">
    <ItemTemplate>
    <asp:HiddenField ID="hfBID" runat="server" Value='<%#Eval("Prdno")%>' />
    <asp:Label ID="Prdno" runat="server" Text='<%#Bind("Prdno")%>'></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="Item Name">
    <ItemTemplate>
    <asp:Label ID="itemName" runat="server" Text='<%#Bind("Descriptionitem")%>'></asp:Label>
    </ItemTemplate>
    <FooterTemplate>
    <asp:Label ID="lblTotalText" Text="Total" runat="server"></asp:Label>
    </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="QTY">
    <ItemTemplate>
    <asp:Label ID="QTY1" runat="server" Text='<%#Bind("QTY1")%>'></asp:Label>
    </ItemTemplate>
    <FooterTemplate>
    <asp:Label ID="lblTotal" runat="server"></asp:Label>
    </FooterTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
    </div>
    </td>
    <td>&nbsp;</td>
    </tr>
    </table>
    </div>
    </div>

    Code-Behind:

    protected void bind()
            {
                string str, strSql;
                str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542ConnectionString"].ConnectionString;
                SqlConnection conn = new SqlConnection(str);
                strSql = "select * from Test2";
                SqlDataAdapter da = new SqlDataAdapter(strSql, str);
                DataSet ds = new DataSet();
                da.Fill(ds, "Test2");
                GVallbb.DataSource = ds.Tables[0].DefaultView;
                GVallbb.DataBind();
                conn.Close();
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bind();
                }
    
            }
    
            protected void txtPack_TextChanged(object sender, EventArgs e)
            {
                string value = txtPack.Text;
                for (int i = 0; i < GVallbb.Rows.Count; i++)
                {
                    string cellvalue = ((Label)GVallbb.Rows[i].FindControl("Prdno")).Text.ToString();
                    if (cellvalue == value)
                    {
                        CheckBox cb = (CheckBox)GVallbb.Rows[i].FindControl("CheckBox1");
                        cb.Checked = true;
                        GVallbb.Rows[i].BackColor= Color.Green;
                    }
                }
            }

    Result:

    Best regards,

    Yijing sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 6, 2020 3:11 AM
  • User-367318540 posted

    Hi Yij Sun,

    Yes i want  to retrieve database based on a field and set default checked row?

    Monday, January 6, 2020 4:13 AM
  • User1535942433 posted

    Hi akhterr,

    Accroding to your description,I couldn't understand your requirment clearly.

    Do you want to complete the default selected row when the page load?

    What is the condition for checkbox to check if it should be checked?

    Whether there is a field bound checkbox in the database? if it is true, the checkbox will be  selected.

    Could you tell us more details about your requirment?It will help us solve your problem more faster.

    Best regards,

    Yijing Sun

    Tuesday, January 7, 2020 5:29 AM