Gridview TemplateField

1

I need to change a specific cell in GridView , but when I try to do this in RowDataBound :

 TextBox x = ((TextBox)e.Row.Cells[i].FindControl("ctl" + tx.PadLeft(2,'0')))
 x.Enable = False;

or this way;

((TextBox)e.Row.Cells[i].FindControl("ctl")).Enable = false;

or this:     e.Rows [i] .Cells [j] .Enable = false;

The whole column is affected, can someone help me change a cell specified for a specific row / column.

My GridView Has the following HTML:

<asp:GridView ID="GridView1" runat="server" CellPadding="4" Font-Size="XX-Small" ForeColor="#333333" Width="100%" HorizontalAlign="Center" PageSize="35" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="False">
        <AlternatingRowStyle BackColor="White" ForeColor="#6E7265" />
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#BDC0C4" Font-Bold="True" ForeColor="#333333" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="White" ForeColor="#333333" Wrap="True" BorderStyle="Double" BorderWidth="1px" HorizontalAlign="Center" VerticalAlign="Middle" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" ForeColor="White" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>

In html they can not dazzle the templteeField because it is created in the code behind as follows:

 foreach (DataColumn col in x.Columns)
            {
                //Declare the bound field and allocate memory for the bound field.
                TemplateField bfield = new TemplateField();

                //Initalize the DataField value.
                bfield.HeaderTemplate = new GridViewTemplate(ListItemType.Header, col.ColumnName);

                //Initialize the HeaderText field value.
                bfield.ItemTemplate = new GridViewTemplate(ListItemType.Item, col.ColumnName);

                //Add the newly created bound field to the GridView.
                GridView1.Columns.Add(bfield);
            }
    
asked by anonymous 24.06.2014 / 20:03

3 answers

1

If I'm not mistaken I think it was like this:

 DataGridView1.Rows(e.RowIndex).Cells(0).Value = valorquevocêquiser;
    
24.06.2014 / 20:18
1

Maybe your problem is when rendering the TextBox.

I made an example with the data you sent (HMTL + C #). In this example, I changed how to add custom fields to gridview . See below:

The same HTML of the gridview that you submitted, plus the addition of the custom field txtCustom .

<asp:GridView ID="grid" runat="server" CellPadding="4" Font-Size="XX-Small" 
    ForeColor="#333333" Width="100%" HorizontalAlign="Center" PageSize="35" 
    AutoGenerateColumns="False" OnRowDataBound="grid_RowDataBound">
    <AlternatingRowStyle BackColor="White" ForeColor="#6E7265" />
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#BDC0C4" Font-Bold="True" ForeColor="#333333" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="White" ForeColor="#333333" Wrap="True" BorderStyle="Double" BorderWidth="1px" HorizontalAlign="Center" VerticalAlign="Middle" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#E9E7E2" />
    <SortedAscendingHeaderStyle BackColor="#506C8C" ForeColor="White" />
    <SortedDescendingCellStyle BackColor="#FFFDF8" />
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="txtCustom" runat="server" Text="Items" Enabled="false"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Capturing event RowDataBound :

protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var ctrl = e.Row.Cells[0].FindControl("txtCustom") as TextBox;
        ctrl.Enabled = false;
    }
}

Notice the if (e.Row.RowType == DataControlRowType.DataRow) . This check is necessary because I want to change only the rows of data, I am ignoring the header, footer, among others.

    
24.06.2014 / 20:36
1

I researched a little more and realized that you used this example to create the columns dynamically in your gridview. To find the correct control in the cell, you need a few steps:

1.Apply an ID for the custom control in GridViewTemplate:

public void InstantiateIn(System.Web.UI.Control container)
{
    // Create the content for the different row types.
    switch (templateType)
    {
        case DataControlRowType.DataRow:
            var textBox = new TextBox();
            textBox.Text = "Default Value";
            textBox.Enabled = true;
            textBox.ID = "txtCustom";
            container.Controls.Add(textBox);
            break;

        default:
            // Insert code to handle unexpected values.
            break;
    }
}

In my case, I just used DataRow again to add a custom control.

2. HTML does not change:

<asp:GridView ID="grid" runat="server" CellPadding="4" Font-Size="XX-Small" 
    ForeColor="#333333" Width="100%" HorizontalAlign="Center" PageSize="35" 
    AutoGenerateColumns="False" OnRowDataBound="grid_RowDataBound">
    <AlternatingRowStyle BackColor="White" ForeColor="#6E7265" />
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#BDC0C4" Font-Bold="True" ForeColor="#333333" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="White" ForeColor="#333333" Wrap="True" BorderStyle="Double" BorderWidth="1px" HorizontalAlign="Center" VerticalAlign="Middle" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#E9E7E2" />
    <SortedAscendingHeaderStyle BackColor="#506C8C" ForeColor="White" />
    <SortedDescendingCellStyle BackColor="#FFFDF8" />
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>

3. You can find the control by name in RowDataBound:

protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var ctrl = e.Row.Cells[0].FindControl("txtCustom") as TextBox;
        ctrl.Enabled = false;
    }
}

In this case, you need to know what column the control is in. The RowDataBound navigates line by line, so you should select the line you want to change and the column.

    
24.06.2014 / 21:28