Moses E
Moses E

Reputation: 35

To bind grid view with textbox

In the below code i have a session value in which i have to pass to the grid and bind the values.The grid consists of textboxes if the session values is 2 there should be two row of textbox.I tried it throws index was out of range.Pls help me to over this issue.

int GoodsReceivedNoteID = (int)Session["GoodsReceivedNoteID"];
for (int iRow = 0; iRow < GoodsReceivedNoteID; iRow++)
{
    TextBox txtFreightCharges = (TextBox)gvGRN.Rows[iRow].Cells[6].FindControl("txtFreightCharges");
    TextBox txtLoadingCost = (TextBox)gvGRN.Rows[iRow].Cells[6].FindControl("txtLoadingCost");
    TextBox txtUnloadingCost = (TextBox)gvGRN.Rows[iRow].Cells[6].FindControl("txtUnloadingCost");
    TextBox txtInsuranseCost = (TextBox)gvGRN.Rows[iRow].Cells[6].FindControl("txtInsuranseCost");
    TextBox txtOtherExpenses = (TextBox)gvGRN.Rows[iRow].Cells[6].FindControl("txtOtherExpenses");
}

Upvotes: 0

Views: 219

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460108

Don't use Rows[index].Cells[6].FindControl, the cell is not the NamingContainer of a control in a GridViewRow's TemplateField but the row itself. I also don't understand the relation between your session value and the number of rows in the grid. This is simpler and more readable:

foreach(GridViewRow row in gvGRN.Rows)
{
    TextBox txtFreightCharges = (TextBox)row.FindControl("txtFreightCharges");
    TextBox txtLoadingCost = (TextBox)row.FindControl("txtLoadingCost");
    TextBox txtUnloadingCost = (TextBox)row.FindControl("txtUnloadingCost");
    TextBox txtInsuranseCost = (TextBox)row.FindControl("txtInsuranseCost");
    TextBox txtOtherExpenses = (TextBox)row.FindControl("txtOtherExpenses");
}

If you only want to take GoodsReceivedNoteID-rows (which sounds wrong since an ID is not a counter):

for(int i = 0; i < GoodsReceivedNoteID; i++))
{
    GridViewRow row = gvGRN.Rows[i];
    TextBox txtFreightCharges = (TextBox)row.FindControl("txtFreightCharges");
    // ...
}

Upvotes: 1

Related Questions