4
Answers

Drop down list problem for editing req in grid view

Becky Bloomwood

Becky Bloomwood

14y
2.6k
1
Hi, I am trying to do an edit req and when I click on the edit, one of the options is DDL. However, there is a prob. I am not sure of how to resolve it.
This is the business logic for the DDL:

 

protected void gvChecklist_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList cmbStatus = (DropDownList)e.Row.FindControl("cmbStatus");
cmbStatus.DataSource = vrmdb.Get_Status();
cmbStatus.DataTextField = "status";
cmbStatus.DataValueField = "status";
cmbStatus.DataBind();
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList cmbNewStatus = (DropDownList)e.Row.FindControl("cmbNewStatus");
if (cmbNewStatus != null)
{
cmbNewStatus.DataSource = vrmdb.Get_Status();
cmbNewStatus.DataTextField = "status";
cmbNewStatus.DataValueField = "status";
cmbNewStatus.DataBind();
cmbNewStatus.SelectedValue = gvChecklist.DataKeys[e.Row.RowIndex].Values[1].ToString();
};
}
}
protected void gvChecklist_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList cmbStatus = (DropDownList)e.Row.FindControl("cmbStatus");
cmbStatus.DataSource = vrmdb.Get_Status();
cmbStatus.DataTextField = "status";
cmbStatus.DataValueField = "status";
cmbStatus.DataBind();
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList cmbNewStatus = (DropDownList)e.Row.FindControl("cmbNewStatus");
if (cmbNewStatus != null)
{
cmbNewStatus.DataSource = vrmdb.Get_Status();
cmbNewStatus.DataTextField = "status";
cmbNewStatus.DataValueField = "status";
cmbNewStatus.DataBind();
cmbNewStatus.SelectedValue = gvChecklist.DataKeys[e.Row.RowIndex].Values[1].ToString();
};
}
}

This is the error:

 

Server Error in '/VRM_WebSite' Application.

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Source Error:


Line 417:                    cmbNewStatus.DataValueField = "status";
Line 418:                    cmbNewStatus.DataBind();
Line 419:                    cmbNewStatus.SelectedValue = gvChecklist.DataKeys[e.Row.RowIndex].Values[1].ToString();
Line 420:                };
Line 421:            }

Source File: c:\Users\L31410\Desktop\Peggie\Peggie\Deployment Source\Phase 1\Deployment 25 Nov for StarHub\VS Projects Source\VRM_WebSite\app\vrm\ContractThreeGridView.aspx.cs    Line: 419

Stack Trace:


Thks for ur help all these while. I am grateful for it:)













 
 
Answers (4)
0
Amit Choudhary
NA 27.7k 3m 14y
HI friend,

gvChecklist.DataKeys[e.Row.RowIndex].Values[1].ToString()
I have doubt on this statement. Did you set the Datakeys property of your grid in designer?? if you have already set it then make sure. .it should have two fields in comma separated form cause you have use value[1]. means here you are attempting to access the second indexed value from datakey collection.
if you are using only one field as datakey then use Value[0].

Thanks..


Accepted
0
Becky Bloomwood
NA 119 240.2k 14y
Sure, no prob. I have mark it as accepted answer:D
0
Amit Choudhary
NA 27.7k 3m 14y
Becky, Now i'm looking to you to mark my post as answer.. if you do believe in favor back.. :))
0
Becky Bloomwood
NA 119 240.2k 14y
Thanks. It works like a charm:)I am grateful to u guys!Cheers!