user635545
user635545

Reputation: 111

checkBox in gridView

my following code is not working properly...the code is about when the user check the check Box attached with a grid view this information should trigger to the table and,as soon as the data is send to database and two views will be created depending on the condition if check box is check or not..and the result of the view should be display in a label box.(IN ASP.NET USING C# AND DB-MSSQL)

CREATE TABLE yourBaseTable
  (
   ID INT IDENTITY(1, 1)
       PRIMARY KEY ,
   Payload NVARCHAR(255) NOT NULL ,
   Discriminator TINYINT NOT NULL
              CHECK ( Discriminator IN ( 0, 1 ) )
              DEFAULT ( 0 )
  ) ;
GO

CREATE VIEW yourView1
AS
  SELECT ID ,
      Payload ,
      Discriminator
  FROM  yourBaseTable
  WHERE  Discriminator = 0 ;
GO

CREATE VIEW yourView2
AS
  SELECT ID ,
      Payload ,
      Discriminator
  FROM  yourBaseTable
  WHERE  Discriminator = 1 ;
GO

and i am havind the front end problem too

The following is description of method 'checkBox_checkedChange'..and i donot get a line of code

CheckBox is clicked the CheckBox_CheckedChanged event is fired. We need to implement this method in order to catch the row in which the CheckBox is checked.

protected void CheckBox1_CheckedChanged(object sender, EventArgs e) {

ClearCheckBoxes();

CheckBox checkbox = (CheckBox)sender;

checkbox.Checked = true;

GridViewRow row = (GridViewRow)checkbox.NamingContainer; // WHAT DOES THIS CODE MEANS ..

DisplayMessage(row.Cells[1].Text);

}

Upvotes: 0

Views: 1587

Answers (2)

Starwfanatic
Starwfanatic

Reputation: 614

Ok, I've never dealt with views through SQL. I handle views through my queries. Here's a sample of how I would handle this. I think it does what you are looking for. This is my complete code. You should be able to copy paste to see it run with the exception of me using linq to sql. You may need to translate my queries to SQL, but I highly recomment Linq-to-SQL. It's soo much easier to work with.

aspx page:

<form id="form1" runat="server">
<div>
    <asp:GridView ID="grdSelected" runat="server" AutoGenerateColumns="False" EnableModelValidation="True" HeaderStyle-BackColor="#1B940A">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="lblCheckedID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="chkSelected" runat="server" Checked='<%# Bind("Discriminator") %>'
                        OnCheckedChanged="chkSelected_CheckedChanged" AutoPostBack="true" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Payload") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

    <asp:GridView ID="grdNotSelected" runat="server" AutoGenerateColumns="False" EnableModelValidation="True" HeaderStyle-BackColor="#84090C">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="lblCheckedId" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="chkNotSelected" runat="server" Checked='<%# Bind("Discriminator") %>'
                        OnCheckedChanged="chkSelected_CheckedChanged" AutoPostBack="true" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Payload") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</div>
</form>

Code behind:

public partial class _Default : System.Web.UI.Page
{
    SampleConnectionClassDataContext sampleContext = new SampleConnectionClassDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCheckedGrid();
            BindNonCheckedGrid();
        }
    }

    private void BindCheckedGrid()
    {
        var checkedItems = from check in sampleContext.CheckboxSamples
                           where check.Discriminator == true
                           select check;

        grdSelected.DataSource = checkedItems;
        grdSelected.DataBind();
    }

    private void BindNonCheckedGrid()
    {
        var checkedItems = from check in sampleContext.CheckboxSamples
                           where check.Discriminator == false
                           select check;

        grdNotSelected.DataSource = checkedItems;
        grdNotSelected.DataBind();
    }


    protected void chkSelected_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox checkbox = (CheckBox)sender;
        GridViewRow row = (GridViewRow)checkbox.NamingContainer;
        Label lblID = (Label)row.FindControl("lblCheckedID");

        var existingEntry = from s in sampleContext.CheckboxSamples
                            where s.ID == int.Parse(lblID.Text)
                            select s;

        existingEntry.First().Discriminator = checkbox.Checked;
        sampleContext.SubmitChanges();

        BindCheckedGrid();
        BindNonCheckedGrid();

    }
}

Hope this helps :-)

Upvotes: 0

Starwfanatic
Starwfanatic

Reputation: 614

I'm unsure exactly what you're asking in the first part. As far as the question at the bottom:

GridViewRow row = (GridViewRow)checkbox.NamingContainer;//WHAT DOES THIS CODE MEANS ..

The ".NamingContainer" will get the object that contains your checkbox, in this case your checkbox is inside a GridViewRow. You still have to cast the object as a GridViewRow which is the purpose of the "(GridViewRow)". You are assigning it to a local variable row so that you can continue to work with it.

Upvotes: 2

Related Questions