开发者

How to Check the CheckBox in a GridView based on Condition?

i am a beginner. I have four textbox fields for ItemName, ItemDescription, Length and Quantity. One radiobuttonlist and its listitems are FixedLength and Random Length. One button field called Submit and one gridview.

In the GridView I have 4 bound columns and 1 template column such as ItemName, ItemDescription, Length, Quantity and IsFixed_f(flag field). I have added one checkbox in the GridView's Template column for the IsFixed_f field.

After entering all textbox fields, I have to select either one listitem in the radiobuttonlist(FixedLength or RandomLength). After completing these things. If I enter the Submit button, all the values in the textbox to be displayed under the GridView's corresponding column name and checkbox to be checked if FixedLength listitem is selected otherwise the checkbox.checked should be false. How to do this?

In the Button Click event, i have used like this

        DataRow DR = null;
        DR = datatable.NewRow();
        DR["ItemName"] = DSItemName.Text.Trim();
        DR["Description"] = txtItemDescription.Text.Trim();
        DR["Length"] = txtLength.Text.Trim();
        DR["Q开发者_StackOverflowuantity"] = txtQuantity.Text.Trim();
        datatable.Rows.Add(DR);
        GridView.DataSource = datatable;
        GridView.DataBind();

But I dont know How to Check the CheckBox and Displays it in the GridView. Please give suggestions. If I use the following code, It displays the text "true" with the CheckBox in the GridView.

        foreach (GridViewRow GVR in gridview.Rows)
        {
            CheckBox cb = (CheckBox)GVR.FindControl("cbIsFixed_f");

            if (cb != null && radiobuttonlist.SelectedItem.Value == "Fixed Length")
            {
                cb.Checked = true;
            }
            else
            {
                cb.Checked = false;
            }
            DR["IsFixed_f"] = cb.Checked;
        }


You can write an event handler for the gridview's OnRowDataBound event. In that event you need to find the checkbox column and set it checked or unchecked.

something like this:

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
  CheckBox c = e.Row.Cells[4].FindControl("YourCheckboxId");
  if(c != null && e.Row.DataItem["sFixed_f"] == true)
  {
    c.Checked = true;
  }
}


I am not quite sure how to get a row out of a dataset - I assume you meant dataTable.newRow(). Furthermore I hope your tableSchema contains a column for your IsFixed_f values.

If your tableSchema has an extra column for your IsFixed_f values you may bind those values by using a checkBoxField.

As you mentioned a templateField I assume your table does not have any definition for IsFixed_f? In this case you may manually add this column to your schema.

Binding your IsFixed_f value to a specific checkbox (in a new gridviewrow) without storing it somewhere (dataset/table?!) - seems not possible to me...

Hope this might help - if not, please describe your problem in detail!


I got the answer for this question.

 DR["IsFixed_f"] = radiobuttonlist.SelectedItem.Value == "Fixed Length" ?true : false;

thank u all for ur responses.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜