c# - ASP.Net 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;
}
请问有什么想法吗?谢谢你!
解决方案
推荐阅读
- python - 匹配来自两个字典或列表的值
- arrays - 有没有办法从教堂的数组中打印格式化的表格?
- user-defined-functions - Snowflake Java Script 用户定义表函数
- spring - Kotlin 中实体的延迟加载
- node.js - 您可以命名 Puppeteer 生成的 PDF 而不保存它吗?
- flutter - Flutter Route 动画为滑动的新旧路线设置动画
- angular - How to access the angular application route with parameter from another application
- python - 单击:如何获取用于自定义帮助输出的命令的所有选项?
- r - correct way to use dplyr::arrange with dynamic sort by columns
- angular - STLLoader 是否接受 Web URL 并显示文件?