Gridview Templatefield

Asked

Viewed 1,945 times

1

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

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

or in this way;

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

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

The entire spine is affected, someone can help me change one cell specifies 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 cannot dazzle templteeField because it is created in Behind code 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);
            }
  • In which gridview event you are trying to change the cell value?

  • I’m trying to change in Rowdatabound

  • Supplement your question with the HTML of gridview, I think it’ll be easier to understand.

3 answers

1

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

 DataGridView1.Rows(e.RowIndex).Cells(0).Value = valorquevocêquiser;
  • The problem is that in this location I have a textbox, and my intention is to change the Enable of the text only in this cell.

  • But this text gets some ID??

  • yes gets, "cl"+j and the column number

  • And wouldn’t it be possible to just make this change by the ID instead of the cell? Like this: textBox1.Enabled = false

  • yes, I try to do it by id, but eventually I change the whole column.

  • If you try to apply by id, you will apply a value to all Textbox that has the same id. This change, so that it occurs individually (per cell), must be done in Rowdatabound.

  • is where I make the change

  • It’s what I suspected, I had a problem like that putting together a development documentation report and did with gridView. And I really know this code here and it was meant to work: ((Textbox)Gridview1.Rows[index].Cells[0].Findcontrol("txtPerguntaRepeater")). Enable = value

  • how did you solve the problem?

  • I did everything in hand without Gridview I was assembling in html the results one by one. Creating the table in hand. It’s because I had a deadline and I brought the information of a Sharepoint DB, so I had to do gambiarra! : P

  • I think there must be a better way to do that. The problem is that it’s a dynamic table

  • Yes and I think you are in the way, but it is complicated to use gridView, I hope never need depending on the requested development.

Show 7 more comments

1

Maybe your problem is at the time of Textbox rendering.

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

Same gridview HTML you sent, plus adding 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>

The capture of the 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;
    }
}

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

  • Yes but the number of columns is not always the same. and this would change me all rows where this id appears. What’s my problem.

1

I did some more research and I realized you used this example to create columns dynamically in your gridview. To find the correct control in the cell, a few steps are needed:

1.Apply a custom control ID to 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 only 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 find 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, it is necessary that you know which column the control is. The RowDataBound navigate row by row, then you should select the row you want to change and the column.

  • Multiple controls have been found with the same txtValor ID. Findcontrol requires controls to have unique ID. This Solution still does not work

  • But have you filtered the lines you want to modify the values? Rowdatabound will run through all gridview lines and for each row you will find only one txtCustom in column 0 (considering the example I did).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.