Click here to Skip to main content
16,018,818 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I tried the followning but the error come

protected void GridApprove_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{

Label lbl_pending = new Label();
Label lbl_approved = new Label();
int val_approved = Convert.ToInt32(e.Row.Cells[17].ToString());
int val_checked = Convert.ToInt32(e.Row.Cells[18].ToString());
if (val_approved == 2 && val_checked == 2)
{
Button btn_accept = new Button();
Button btn_discard = new Button();
btn_accept.Text = "ACCEPT";
btn_discard.Text="DISCARD";
e.Row.Cells[7].Controls.Add(btn_accept);
e.Row.Cells[7].Controls.Add(btn_discard);
}

//tbox.Text= "the text you wasnt to add or bind with data field";
//e.Row.Cells[7].Controls.Add(tbox);
}


The exception generated that {"Specified argument was out of the range of valid values.\r\nParameter name: index"}

When I retrive the data in data table in that in the 17 column and 18 column the value I want to fetch so plz help me
Posted
Updated 27-Jun-13 1:21am
v2
Comments
RelicV 27-Jun-13 5:57am    
You could add both the Button and Label controls to the column and based on the condition isApproved = true, make the Button visible and the Label invisible (using style - display:none) or vice-versa.
Member 10076060 27-Jun-13 7:24am    
I also tried that but the adding control is not display in the grid view

1 solution

Below is the reference code of one way to achieve this.

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"
            Height="100px">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button ID="btn1" runat="server" Text="Approve" CommandArgument= />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>


protected void Page_Load(object sender, EventArgs e)
     {
         string [] dataSource = { "3","1", "2" };
         if (!IsPostBack)
         {
             GridView1.DataSource = dataSource;
             GridView1.DataBind();
         }
     }

     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
     {

         if (e.Row.RowType == DataControlRowType.DataRow)
         {
             if (e.Row.DataItem.ToString() == "1")
             {
                 var cont = e.Row.FindControl("btn1");
                 e.Row.Cells[0].Controls.Remove(cont);
                 e.Row.Cells[0].Text = "Not Approved";
             }
         }
     }
 
Share this answer
 

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