How can I access the DataGridRow from the textbox on this row?

In DataGrid, when the text in the textbox changes, I want to add the value of another field to that row in the array.

public void txtTitle_TextChanged(object sender, EventArgs e)
{
    TextBox titleBox = (TextBox)sender;
    DataGridItem myItem = (DataGridItem)titleBox.Parent.Parent;
    string test = DataBinder.Eval(myItem.DataItem, "prod_id").ToString();
}

      

However, myItem.DataItem evaluates to null. I expected it to evaluate as a DataRowView?

0


a source to share


2 answers


Effectively I solved this by adding an autonumber column to the table and using the this value to define the positino row in the table and then using the value of that to affect the corresponding row in the datagrid. Now I am just changing the color of the string rather than adding the values ​​to that string to the array as stated in the original question.



public void txtPrice_TextChanged(object sender, EventArgs e)
{
    TextBox txtPrice = (TextBox)sender;
    DataGridItem myItem = (DataGridItem)txtPrice.Parent.Parent;
    markRows(myItem, true);
}

public void markRows(DataGridItem myItem, bool toSave)
{
    // Prepeare to save this record?
    CheckBox thisSave = (CheckBox)myItem.FindControl("chkSave");
    thisSave.Checked = toSave;
    // Establish the row position in the table
    Label sNo = (Label)myItem.FindControl("SNo");
    int rowNum = Convert.ToInt32(sNo.Text) - 1;
    CheckBox rowSave = (CheckBox)grid.Items[rowNum].FindControl("chkSave");

    // Update background color on the row to remove/add highlight 
    if (rowSave.Checked == true)
        grid.Items[rowNum].BackColor = System.Drawing.Color.GreenYellow;
    else
    {
        Color bgBlue = Color.FromArgb(212, 231, 247);
        grid.Items[rowNum].BackColor = bgBlue;
        // some code here to refresh data from table?
    }
}

      

0


a source


You can get the TextChanged event if you do the following:

<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False" 
    onitemdatabound="DataGrid1_ItemDataBound">
    <Columns>
        <asp:TemplateColumn HeaderText="Test">
            <ItemTemplate>
                <asp:TextBox OnTextChanged="txtBox_TextChanged" ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateColumn>
        <asp:BoundColumn DataField="Name" HeaderText="Test 1"></asp:BoundColumn>
    </Columns>
</asp:DataGrid>

      

You will notice that I have the following properties: AutoPostBack = "True" I also manually added OnTextChanged = "txtBox_TextChanged" to the textbox.

In my code that I have:



protected void txtBox_TextChanged(object sender, EventArgs e)
{
    TextBox txtBox = (TextBox)sender;
    Label1.Text = txtBox.Text;
}

      

The only way the event will happen is to lose focus on the textbox after typing.

Key points to consider: This will cause the message to be bounced, so Ajax can be a good way to keep the UI nice. You will need to make sure you complete your DataBind () in if (! IsPostBack)

Hope this helps!

+1


a source







All Articles