1

I have a main 'Products' table and a 'Products_Recommended' table. I want the users to be able to select several products from a GridView using checkboxes and then insert those Product IDs (prodid) into the Products_Recommended table in such a way that a main Product ID is entered (coming from query string) and potentially several recommended ProdIDs get entered. So far so good. But I need to be able to show the checkboxes to be checked if there were already prodids in the Products_Recommended table previously. The code below show a 'sqldatasource1' which gets the data from the Products_Recommended table based on query string. I just don't know how the checkboxes can get checked because the GridView has a different sqldatasource binding it. Thanks! Meengla

  <form id="form1" runat="server">
<asp:GridView ID="Products" runat="server" AutoGenerateColumns="False" DataKeyNames="prodid"
    DataSourceID="alldata" EnableModelValidation="True">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="ProductSelector" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField  DataField="itemnumber" HeaderText="Item Number" SortExpression="itemnumber" />

        <asp:BoundField DataField="itemtitle" HeaderText="itemtitle" SortExpression="itemtitle" />
    </Columns>
</asp:GridView>
<p>
    <asp:Button ID="SelectedProducts" runat="server" Text="Recommend" OnClick="SelectedProducts_Click" />
</p>
<p>
    <asp:Label ID="lblProdSelected" runat="server" EnableViewState="False" Visible="False"></asp:Label>
</p>
<asp:SqlDataSource ID="alldata" runat="server" ConnectionString="<%$ ConnectionStrings:dbconnection %>"
    SelectCommand="SELECT * FROM Products">
    <SelectParameters>
        <asp:QueryStringParameter DefaultValue="14" Name="itemid" QueryStringField="itemid"
            Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbconnection %>"
    SelectCommand="SELECT * FROM dbo.products_recommended WHERE prodid = @itemid)">
    <SelectParameters>
        <asp:QueryStringParameter DefaultValue="14" Name="itemid" QueryStringField="itemid"
            Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

1 Answer 1

1

Handle the RowDataBound event, and in the method find the checkbox and set its Checked value.

<asp:GridView ID="Products" OnRowDataBound="GridViewRowEventHandler">

void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
  if(e.Row.RowType == DataControlRowType.DataRow)
  {
    var ProductSelector = e.Row.FindControl("ProductSelector") as CheckBox;
    ProductSelector.Checked = true;
  }
}

You can use the Select method of DataSource to retrieve the data you need

Sign up to request clarification or add additional context in comments.

9 Comments

Okay. Let me try your solution. Thanks!
Hi, but the Gridview itself is bound to sqldatasource with id 'alldata' while the checkboxes need to be populated or not based on the values from the sqldatasource with id of 'SqlDataSource1'. So your solution will not work?
@Meengla You can use the Select method of DataSource to retrieve the data you need
but how? I have been stuck half a day so if easy to point out then please do so.
You should read the documentation about this method, and you didn't provide any info about rules for the checking/unchecking your checkboxes
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.