我绑定了gridview,它在OnRowEditing事件除外的网格触发的事件中可以正常工作 用户在哪个行上单击“编辑”按钮,直到该行网格已绑定,但以下行的其余其余部分未绑定,因为它抛出了异常“索引0为负或行数以上”错误 下面是我尝试过的代码
aspx网格标记
<asp:GridView ID="grdPharmacy" runat="server" AutoGenerateColumns="false" ShowFooter="true" EnableViewState="true" Width="97%" OnRowDataBound="grdPharmacy_RowDataBound" OnRowUpdating="grdPharmacy_RowUpdating" OnRowEditing="grdPharmacy_RowEditing"
OnRowCancelingEdit="grdPharmacy_RowCancelingEdit" DataKeyNames="srno">
<Columns>
<asp:TemplateField HeaderText="Select Source" HeaderStyle-Width="100px"><ItemTemplate><%#Eval("[sourcename]") %></ItemTemplate>
<EditItemTemplate>
<asp:HiddenField ID="hdnsource" runat="server" Value='<%#Eval("[source]") %>'/>
<asp:DropDownList ID="ddlEditSource" AutoPostBack="true" EnableViewState="true" runat="server" OnSelectedIndexChanged="ddlEditSource_SelectedIndexChanged" Width="100px"></asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlFooterSource" AutoPostBack="true" EnableViewState="true" runat="server" OnSelectedIndexChanged="ddlFooterSource_SelectedIndexChanged" Width="100px"></asp:DropDownList>
</FooterTemplate>
<HeaderStyle Width="10%"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Select Medicine Name" HeaderStyle-Width="200px">
<ItemTemplate>
<asp:Label ID="lblMedicineName" runat="server" Text='<%#Eval("[Medicine Name]") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField ID="hdnMedicineName" runat="server" Value='<%#Eval("[Medicine Name]") %>' ></asp:HiddenField>
<asp:DropDownList ID="ddlEditMedicineList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlEditMedicineList_SelectedIndexChanged" EnableViewState="true" Width="200px"></asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlFooterMedicineList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlFooterMedicineList_SelectedIndexChanged" EnableViewState="true" Width="200px"></asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
</column>
</asp:GridView>
下面是gridview的事件
protected void grdPharmacy_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && grdPharmacy.EditIndex == e.Row.RowIndex)
{
DropDownList ddlEditSource = (DropDownList)e.Row.FindControl("ddlEditSource");
HiddenField hdnsource = (HiddenField)e.Row.FindControl("hdnsource");
DropDownList ddlEditMedicineList = (DropDownList)e.Row.FindControl("ddlEditMedicineList");
HiddenField hdnMedicineName = (HiddenField)e.Row.FindControl("hdnMedicineName");
if (ddlEditSource != null)
{
ddlEditSource.Items.Clear();
DataTable dt = objBALRGJPreauth.BAL_GetMedicineSupplierList();
if (dt.Rows.Count > 0)
{
ddlEditSource.DataTextField = "SupplierName";
ddlEditSource.DataValueField = "SupplierCode";
ddlEditSource.DataSource = dt;
ddlEditSource.DataBind();
try
{
ddlEditSource.Items.FindByValue(hdnsource.Value.ToString()).Selected=true;
}
catch (Exception)
{
}
}
}
if (ddlEditMedicineList != null && !string.IsNullOrEmpty(hdnsource.Value))
{
ddlEditMedicineList.Items.Clear();
DataTable dt = objBALRGJPreauth.BAL_GetMedicineList(ddlEditSource.SelectedValue.ToString());
if (dt.Rows.Count > 0)
{
ddlEditMedicineList.DataTextField = "MedicineName";
ddlEditMedicineList.DataValueField = "MedicineCode";
ddlEditMedicineList.DataSource = dt;
ddlEditMedicineList.DataBind();
try
{
ddlEditMedicineList.Items.FindByText(hdnMedicineName.Value.ToString()).Selected = true;
}
catch (Exception)
{
ddlEditMedicineList.Items.FindByText("Other").Selected = true;
}
}
}
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList ddlFooterSource = (DropDownList)e.Row.FindControl("ddlFooterSource");
//DropDownList ddlFooterMedicineList = (DropDownList)e.Row.FindControl("ddlFooterMedicineList");
if (ddlFooterSource != null)
{
ddlFooterSource.Items.Clear();
DataTable dt = objBALRGJPreauth.BAL_GetMedicineSupplierList();
if (dt.Rows.Count > 0)
{
ddlFooterSource.DataTextField = "SupplierName";
ddlFooterSource.DataValueField = "SupplierCode";
ddlFooterSource.DataSource = dt;
ddlFooterSource.DataBind();
}
}
}
}
protected void grdPharmacy_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
objBORGJPreauth.Srno = Convert.ToInt32(grdPharmacy.DataKeys[e.RowIndex].Value.ToString());
TextBox lblEditMedicineName = (TextBox)grdPharmacy.Rows[e.RowIndex].FindControl("lblEditMedicineName");
TextBox txtEditMedicalquantity = ((TextBox)grdPharmacy.Rows[e.RowIndex].FindControl("txtEditMedicalquantity"));
TextBox txtEditMedicalRate = ((TextBox)grdPharmacy.Rows[e.RowIndex].FindControl("txtEditMedicalRate"));
TextBox txtEditMedicineDate = ((TextBox)grdPharmacy.Rows[e.RowIndex].FindControl("txtEditMedicineDate"));
DropDownList ddlEditMedicineList = ((DropDownList)grdPharmacy.Rows[e.RowIndex].FindControl("ddlEditMedicineList"));
DropDownList ddlEditSource = ((DropDownList)grdPharmacy.Rows[e.RowIndex].FindControl("ddlEditSource"));
if (txtEditMedicalRate.Text != "" && txtEditMedicalquantity.Text != "")
{
//txtEditmedicinetotal.Text = (Convert.ToDouble(txtEditMedicalRate.Text) * Convert.ToInt32(txtEditMedicalquantity.Text)).ToString();
objBORGJPreauth.Medicines = lblEditMedicineName.Text;
objBORGJPreauth.Medicalquantity = txtEditMedicalquantity.Text;
objBORGJPreauth.MedicalRate = txtEditMedicalRate.Text;
//objBORGJPreauth.medicinetotal = txtEditmedicinetotal.Text;
string[] str = ddlEditMedicineList.SelectedValue.ToString().Split(';');
if (str.Length >= 3)
{
objBORGJPreauth.MedicineCode = str[0].ToString();
}
objBORGJPreauth.Source = ddlEditSource.SelectedValue.ToString();
}
else
{
grdPharmacy.EditIndex = -1;
GetBillingDetailsDetails();
msgBoxSucess.ShowError("Please Enter Medicine Rate And Qunqtity");
return;
}
//@medicinename
objBORGJPreauth.PatCaseNo = ViewState["CaseSrNo"].ToString();
//objBORGJPreauth.Hosp_Ref = ((BOSession)Session["User_Object"]).Hospital_Ref_No;
objBORGJPreauth.SCaseNoSrNo = Convert.ToInt32(ViewState["CaseSrNo"].ToString());
objBORGJPreauth.MedicineDate = txtEditMedicineDate.Text;
objBORGJPreauth.Operation = "update";
DataTable dtBilling = new DataTable();
dtBilling = objBALRGJPreauth.BAL_GetPreAuth_BillingDetails(objBORGJPreauth);
if (dtBilling.Rows.Count > 0)
{
ClientScript.RegisterStartupScript(GetType(), "MessageBox", "<script>Record Inserted</script>");
}
grdPharmacy.EditIndex = -1;
GetBillingDetailsDetails();
}
protected void grdPharmacy_RowEditing(object sender, GridViewEditEventArgs e)
{
grdPharmacy.EditIndex = e.NewEditIndex;
GetBillingDetailsDetails();
}
protected void grdPharmacy_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdPharmacy.EditIndex = -1;
GetBillingDetailsDetails();
}
这就是我在!ispostback中绑定网格的方式
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) { GetBillingDetailsDetails(); }
}
private void GetBillingDetailsDetails()
{
try
{
DataTable dtBilling = new DataTable();
dtBilling = objBALRGJPreauth.BAL_GetPreAuth_BillingDetails(objBORGJPreauth);
if (dtBilling.Rows.Count > 0)
{
//hdnPAN_GV_BIllingDetailsCount.Value = Convert.ToString(dtBilling.Rows.Count);
grdPharmacy.Visible = true;
grdPharmacy.DataSource = dtBilling;
grdPharmacy.DataBind();
}
else
{
grdPharmacy.Visible = true;
DataRow aBlankRow = dtBilling.NewRow();
dtBilling.Rows.Add(aBlankRow);
grdPharmacy.DataSource = dtBilling;
grdPharmacy.DataBind();
// SHOW A SINGLE COLUMN WITH A MESSAGE.
int col = grdPharmacy.Rows[0].Cells.Count;
grdPharmacy.Rows[0].Cells.Clear();
grdPharmacy.Rows[0].Cells.Add(new TableCell());
grdPharmacy.Rows[0].Cells[0].ColumnSpan = col;
grdPharmacy.Rows[0].Cells[0].Text = "Add Pharmacy Details";
}
}
catch (Exception ex)
{
ErrorHandler.WriteError(ex);
}
}
请帮助我解决此问题