首页 > 解决方案 > ASP.Net Gridview 搜索返回不正确的索引

问题描述

我制作了一个gridview表,如下所示: 在此处输入图像描述

问题是在搜索时,我的表返回了不正确的索引,即 0。我知道这是因为我的表在搜索时只返回一行,因此在索引 0 处。但我在解决这个问题时遇到了麻烦。我想要表中值的原始索引。

这是我点击“搜索”时触发的功能:

public void searchDatabaseForMedicine()
    {
        String medicineNameTxt = medicineName.Text;


        int Found;

        if (medicineNameTxt != "" || medicineNameTxt != null)
        {
            Found = myAccount.SearchMedicineStock(medicineNameTxt, ref dataTable);

            if (Found > 0)
            {
                medicineDatabase.DataSource = dataTable;
                medicineDatabase.DataBind();

            }
            else
            {
                medicineDatabase.DataSource = null;
                medicineDatabase.DataBind();
            }
        }
    }

当我在值“int”中选择一个选项卡(这是返回错误索引的选项卡)时调用此函数:

protected void medicineDatabase_SelectedIndexChanged(object sender, EventArgs e)
    {

        int index = int.Parse(medicineDatabase.SelectedIndex.ToString());
        selectedList.Add(index);

        GridViewRow row = medicineDatabase.SelectedRow;



        Label medicineLabel = (Label) medicineDatabase.Rows[index].FindControl("lblMedicine");
        Label costLabel = (Label) medicineDatabase.Rows[index].FindControl("lblPrice");

        Label vendorLabel = (Label)medicineDatabase.Rows[index].FindControl("lblVendor");

        //int costVal = int.Parse(cost.Text);
        String medicineNameTxt = medicineLabel.Text;
        String fullfilledBy = vendorLabel.Text;

        float costFloat = float.Parse(costLabel.Text);

        user = Request.QueryString["user"];

        String cnic = myAccount.getCNIC(user);


        myAccount.addMedicineOrderItem(cnic, costFloat, medicineNameTxt, fullfilledBy);


    }

这是 SearchMedicineStock:

public int SearchMedicineStock(String Name, ref DataTable DT)
    {
        int Found = 0;
        DataSet dataSet = new DataSet();
        SqlConnection connection = new SqlConnection(connString);
        connection.Open();
        SqlCommand cmd;
        try
        {
            cmd = new SqlCommand("searchMedicineStock ", connection); //name of your procedure
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@medicine", SqlDbType.VarChar, 15);
            cmd.Parameters.Add("@Found", SqlDbType.Int).Direction = ParameterDirection.Output;

            // set parameter values
            cmd.Parameters["@medicine"].Value = Name;


            cmd.ExecuteNonQuery();

            // read output value 
            Found = Convert.ToInt32(cmd.Parameters["@Found"].Value); //convert to output parameter to interger format

            if (Found == 1)
            {
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))

                {
                    da.Fill(dataSet);

                }

                DT = dataSet.Tables[0];

            }


            connection.Close();


        }
        catch (SqlException ex)
        {
            Console.WriteLine("SQL Error" + ex.Message.ToString());

        }
        finally
        {
            connection.Close();
        }

        return Found;

    }

请问有什么想法吗?谢谢你!

标签: c#asp.netgridview

解决方案


推荐阅读