简体   繁体   中英

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");
}

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");
    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM