locked
Insert row with different controls in item template at row index one in gridview RRS feed

  • Question

  • User1192570106 posted

    Hi All,

    I have a gridview with item templates containing textboxes. I want first row of grid with checkboxes item template.

    Can any one tell me a good approach to insert that checkboxes row at position one. 
    I don't want create text box at run time, want to keep them in aspx page.

    Thanks in advance.

    Monday, December 4, 2017 10:39 PM

All replies

  • User-1716253493 posted

    There are two way you can use,

    first rowdatabound event to set textboxes and checkboxes visibility based e.row.rowindex,

    second set above controls visibility directly in aspx

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if(e.Row.RowType==DataControlRowType.DataRow)
            {
                TextBox tb = (TextBox)e.Row.FindControl("tb1");
                CheckBox cb = (CheckBox)e.Row.FindControl("cb1");
                if (e.Row.RowIndex > 0)
                {
                    cb.Visible = false;
                    tb.Visible = true;
                }
                else
                {
                    cb.Visible = true;
                    tb.Visible = false;
                }
            }
        }

    2nd way

    <ItemTemplate>
       <asp:CheckBox ID="CheckBox1" runat="server" Visible='<%# Container.DataItemIndex>0 ?false:true %>' />
    </ItemTemplate>
    Tuesday, December 5, 2017 6:41 AM
  • User-707554951 posted

    Hi Pratap09,

    Are you want something as below?

     <asp:GridView ID="GridView1" HeaderStyle-BackColor="#9AD6ED" HeaderStyle-ForeColor="#636363"
                    runat="server" AutoGenerateColumns="false" OnDataBound="OnDataBound">
                    <Columns>
                        <asp:TemplateField HeaderText="CustomerName">
                            <ItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("CustomerName")%>'></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="CustomerCountry">
                            <ItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("CustomerCountry") %>'></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

    CodeBehind:

    using System.Data;
    using System.Drawing;
     protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[4] { new DataColumn("CustomerName"), new DataColumn("CustomerCountry"), new DataColumn("EmployeeName"), new DataColumn("EmployeeCountry") });
                    dt.Rows.Add("John Hammond", "United States", "Albert Dunner", "Bolivia");
                    dt.Rows.Add("Mudassar Khan", "India", "Jason Sprint", "Canada");
                    dt.Rows.Add("Suzanne Mathews", "France", "Alfred Lobo", "Philippines");
                    dt.Rows.Add("Robert Schidner", "Russia", "Shaikh Ayyaz", "UAE");
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
            protected void OnDataBound(object sender, EventArgs e)
            {
                GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
                TableHeaderCell cell = new TableHeaderCell();
                CheckBox ck = new CheckBox();
                ck.ID = "ck1";
                ck.Text = "CheckBox";
                cell.Controls.Add(ck);
                row.Controls.Add(cell);
    
                cell = new TableHeaderCell();
                CheckBox ck2 = new CheckBox();
                ck2.ID = "ck2";
                ck2.Text = "CheckBox2";
                cell.Controls.Add(ck2);
                row.Controls.Add(cell);
    
                row.BackColor = ColorTranslator.FromHtml("#3AC0F2");
                GridView1.HeaderRow.Parent.Controls.AddAt(1, row);
            }

    Output:

    Best regards 

    Cathy

    Tuesday, December 5, 2017 8:38 AM