我正在使用asp gridview元素对数据库进行搜索并使用
返回一些数据 <asp:GridView ID="storedRecordsGrid" AutoGenerateSelectButton="true" emptydatatext="No data available." runat="server">
所以我有一个可以使用popolated的gridview,每行都有自己的select事件。当用户选择一行时,我想使用该行中的数据来填充页面上的texbox。
protected void OnRowCommand(GridViewCommandEventArgs e)
{
}
所以我有这个方法,我有我的文本框,但当选择甚至触发时,我不确定如何为所选行的每个列存储数据。我想拥有的是类似的东西。
protected void OnRowCommand(GridViewCommandEventArgs e)
{
mytextbox.text=datagridcolumn.value
}
我正在使用.net 2.0 而我现在的c#代码看起来像这样。
private DataTable fillGrid(string dComplainantFName,string dComplainantlName)
{
string server = "";
using (SqlConnection cnx = new SqlConnection(server))
using (SqlCommand cmd = new SqlCommand("ComplaintReportLookUp", cnx) { CommandType = CommandType.StoredProcedure })
using (DataTable dt = new DataTable())
{
cmd.Parameters.AddWithValue("@fName", dComplainantFName);
cmd.Parameters.AddWithValue("@lName", dComplainantlName);
try
{
cnx.Open();
dt.Load(cmd.ExecuteReader());
cnx.Close();
return dt;
}
catch (Exception ex)
{
throw new Exception("Error executing MyProcedureName.", ex);
}
}
}
protected void executeGrid(object sender, EventArgs e)
{
storedRecordsGrid.DataSource = this.fillGrid(dComplainantFName.Text,dComplainantLName.Text);
storedRecordsGrid.DataBind();
}
protected void OnRowCommand(GridViewCommandEventArgs e)
{
}
感谢您的帮助
感谢蒂姆的帮助,这就是我最终的工作。
protected void gridSelectedIndexChanged(Object sender, EventArgs e)
{
var row = ((GridView)sender).SelectedRow;
//var cell1Text = row.Cells[0].Text; // should be your autogenerated select cell
dComplainantFName.Text = row.Cells[1].Text.Replace(" ", ""); // Text in column 1 (the first column of your DataTable)
dComplainantLName.Text = row.Cells[2].Text.Replace(" ", ""); // Text in column 2 (the second column of your DataTable)
dComplainantMName.Text = row.Cells[3].Text.Replace(" ", "");
dComplainantAddress.Text = row.Cells[4].Text.Replace(" ", "");
dComplainantCity.Text = row.Cells[5].Text.Replace(" ", "");
dComplainantState.Text = row.Cells[6].Text.Replace(" ", "");
dComplainantZip.Text = row.Cells[7].Text.Replace(" ", "");
dComplainantPhone.Text = row.Cells[8].Text.Replace(" ", "");
storedRecordsGrid.Visible = false;
}
答案 0 :(得分:1)
使用GridView的SelectedIndexChanged
事件,在其他事件中,例如RowCommand
或SelectedIndexChanging
,SelectedRow
属性为null
。这也是符合您要求的适当事件:
protected void Grid_SelectedIndexChanged(Object sender, EventArgs e)
{
var row = ((GridView)sender).SelectedRow;
var cell1Text = row.Cells[0].Text; // is your autogenerated select cell(String.Empty)
var cell2Text = row.Cells[1].Text; // Text in column 2 (the first column of your DataTable)
var cell3Text = row.Cells[2].Text; // Text in column 3 (the second column of your DataTable)
TextBox1.Text = cell2Text; // etc.
}
注意:如果您未将AutoGenerateSelectButton
设置为true并使用TemplateFields
,则应在FindControl
上使用SelectedRow
获取对控件的引用。
如果您仍然坚持使用RowCommand
,则可以从CommandArgument
获取所选行:
protected void Grid_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
var rowIndex = int.Parse(e.CommandArgument.ToString())
var selectedRow = ((GridView)sender).Rows[rowIndex];
var cell1Text = selectedRow.Cells[0].Text; // is your autogenerated select cell(String.Empty)
var cell2Text = selectedRow.Cells[1].Text; // Text in column 2 (the first column of your DataTable)
}
}