[英]How can I get the row selected in my gridview to display in my text boxes?
I have got a Gridview
that display's only certain columns from the database table.我有一个Gridview
只显示数据库表中的某些列。 There is a select button and delete button in my GridView
as well.我的GridView
中也有一个 select 按钮和删除按钮。
This is the code for the GridView
这是GridView
的代码
<asp:GridView ID="GridViewClient" runat="server" DataKeyNames="FirstName"
OnSelectedIndexChanged="GridViewClient_SelectedIndexChanged"
OnRowDeleting="GridViewClient_RowDeleting">
<Columns>
<asp:CommandField HeaderText="Update" ShowSelectButton="True" />
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
</Columns>
</asp:GridView>
Then when the user selects the select
button it should then display the entire table in the text boxes.然后,当用户选择select
按钮时,它应该在文本框中显示整个表格。
This is the code for the select
button:这是select
按钮的代码:
protected void GridViewClient_SelectedIndexChanged(object sender, EventArgs e)
{
EditClient.Visible = true;
GridViewRow row = GridViewClient.SelectedRow;
string constr = @"string";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM tbl_newclient"))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
txtFirstName.Text = sdr["FirstName"].ToString();
txtLastName.Text = sdr["LastName"].ToString();
txtContactNumber.Text = sdr["ContactNumber"].ToString();
txtEmailAddress.Text = sdr["EmailAddress"].ToString();
txtAddress.Text = sdr["Address"].ToString();
txtRestrictions.Text = sdr["Restrictions"].ToString();
txtThirdPartySupport.Text = sdr["ThirdPartySupport"].ToString();
txtBasicNotes.Text = sdr["BasicNotes"].ToString();
}
con.Close();
}
}
Update.Visible = true;
}
The texboxes are hidden on page load and will only be visible on select
What happens now is that when I click on the select
button on any row the first row of the GridView
displays in the text box? texboxes 在页面加载时隐藏,仅在select
上可见 现在发生的情况是,当我单击任何行上的select
按钮时, GridView
的第一行显示在文本框中?
I am not sure what I am doing wrong here?我不确定我在这里做错了什么?
Thanks谢谢
I don't fully understand what you are asking as i can't see all your code but i think the issue may be that you are only reading the first row with the data reader.我不完全理解你在问什么,因为我看不到你所有的代码,但我认为问题可能是你只用数据阅读器阅读第一行。 you could try something like this你可以试试这样的
using (SqlDataReader sdr = cmd.ExecuteReader())
{
if (sdr.HasRows)
{
while (sdr.Read())
{
//I'm not sure what you want to do here? i've just addede to the existing text in the textbox
txtFirstName.Text += sdr["FirstName"].ToString();
txtLastName.Text += sdr["LastName"].ToString();
txtContactNumber.Text += sdr["ContactNumber"].ToString();
txtEmailAddress.Text += sdr["EmailAddress"].ToString();
txtAddress.Text += sdr["Address"].ToString();
txtRestrictions.Text += sdr["Restrictions"].ToString();
txtThirdPartySupport.Text += sdr["ThirdPartySupport"].ToString();
txtBasicNotes.Text += sdr["BasicNotes"].ToString();
}
}
}
I guess you have primarykey
of table tbl_newclient
in your Gridview
.我猜您的primarykey
中有表tbl_newclient
的Gridview
。 Then on select take the primary key from gridview
and append a where condition on your sql query
then it should return the desired data.然后在 select 上,从gridview
和 append 中获取主键,然后sql query
应该返回所需的数据。
protected void GridViewClient_SelectedIndexChanged(object sender, EventArgs e)
{
EditClient.Visible = true;
var primarykey = GridViewClient.SelectedRow.Cells[0].Text; // 0 cell index primary key
string constr = @"string";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM tbl_newclient where yourprimarykey=@primarykey"))
{
cmd.Parameters.AddWithValue("@primarykey", primarykey);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
if (sdr.HasRows)
{
while (sdr.Read())
{
txtFirstName.Text = sdr["FirstName"].ToString();
txtLastName.Text = sdr["LastName"].ToString();
txtContactNumber.Text = sdr["ContactNumber"].ToString();
txtEmailAddress.Text = sdr["EmailAddress"].ToString();
txtAddress.Text = sdr["Address"].ToString();
txtRestrictions.Text = sdr["Restrictions"].ToString();
txtThirdPartySupport.Text = sdr["ThirdPartySupport"].ToString();
txtBasicNotes.Text = sdr["BasicNotes"].ToString();
}
}
}
con.Close();
}
}
Update.Visible = true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.