gpt4 book ai didi

c# - gridview 中的选定行

转载 作者:太空宇宙 更新时间:2023-11-03 16:06:15 25 4
gpt4 key购买 nike

我在sql server中有两个表如下

PatientDetail - 1st table name     
PatientId --- primary key
firstname
lastname

Patexam - 2nd table name
PId ---- foreign key of first table PatientId
Exam

我在页面中有一个 gridview,它显示第一个表的所有列,如下所示

<asp:GridView ID="gvDoctorList" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="PatientId" HeaderText="PatientId" SortExpression="PatientId" />
<asp:BoundField DataField="firstname" HeaderText="firstname" SortExpression="firstname" />
<asp:BoundField DataField="lastname" HeaderText="lastname" SortExpression="lastname" />

<asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" />

</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyDatabaseConnectionString %>" SelectCommand="SELECT [PatientId],[firstname], [lastname], [sex], FROM [PatientDetails]"></asp:SqlDataSource>

我有一个文本值为“formatric3d”的按钮现在,当我在 gridview 中选择一行,然后单击带有

的按钮时
value = "formatric3d",

在点击事件中,我想将所选行 patientid 以及 button text value = "formatric3d" 插入到表名 Patexam 中。

这意味着 PId 等于在 gridview 上选择的 PatientId,而 Exam 等于 button text value = "formatric3d"

最佳答案

这是你想要的吗?

      <asp:Button runat="server" ID="btnExam" Text="formatric3d" OnClick="Exam_ClickHandler" />
<asp:GridView ID="gvDoctorList" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
AllowPaging="True" AllowSorting="True" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server" ID="chk" />
<asp:Label runat="server" ID="lblPID" Visible="false" Text='<%# Eval("PatientId") %>'></asp:Label>
</ItemTemplate>

</asp:TemplateField>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="PatientId" HeaderText="PatientId" SortExpression="PatientId" />
<asp:BoundField DataField="firstname" HeaderText="firstname" SortExpression="firstname" />
<asp:BoundField DataField="lastname" HeaderText="lastname" SortExpression="lastname" />
<asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" />
</Columns>
</asp:GridView>

<h3>Patient Exams</h3>
<asp:DataList runat="server" ID="dtlExams">
<ItemTemplate>
<%#Eval("Exam") %>
</ItemTemplate>
</asp:DataList>
//////////////// tree view
<asp:TreeView runat="server" ID="tvExams">
</asp:TreeView>

在代码隐藏页面上:

  protected void Exam_ClickHandler(object sender, EventArgs e)
{
foreach (GridViewRow row in gvDoctorList.Rows)
{
CheckBox chk = (CheckBox)row.FindControl("chk");
if (chk.Checked)
{
string patientId = ((Label)row.FindControl("lblPID")).Text;
string exam = ((Button)sender).Text;

///your insertion query goes here.
///

GetPatientExams(patientId);
}

}
}

protected void Patient_ExamHandler(object sender, CommandEventArgs e)
{
string patientId = e.CommandArgument.ToString();
GetPatientExams(patientId);


}



private void GetPatientExams(string pid)
{

DataTable exams = "Get exam data from db by pid";

dtlExams.DataSource = exams;
dtlExams.DataBind();

//////////////// TreeView 树节点 tnnn;

    foreach (DataRow row in exams.Rows)
{
tnnn = new TreeNode(exams["PRODSHORT"].ToString());
tvExams.Nodes.Add(tnnn);
}

}

关于c# - gridview 中的选定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19224138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com