0

我有一个如下的网格视图

 <asp:GridView ID="gvDoctorList" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" 
                            AllowPaging="True" AllowSorting="True" 
                             OnSelectedIndexChanged="gvDoctorList_SelectedIndexChanged" OnRowCommand="gvDoctorList_RowCommand" OnRowDataBound="gvDoctorList_RowDataBound">
                            <Columns>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <%--<asp:CheckBox runat="server" ID="chk" OnCheckedChanged="chk_CheckedChanged" AutoPostBack="true" />--%>
                                        <asp:Label runat="server" ID="lblPID" Visible="false" Text='<%# Eval("PatientId") %>'></asp:Label>
                                        <asp:Button ID="btnSelect" runat="server" Text="Select" CommandArgument='<%# Eval("PatientId") %>' CommandName = "Select" />
                                    </ItemTemplate>

                                </asp:TemplateField>

                                <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>

<asp:Button ID="btnformatric" runat="server" Text="formatric3d" OnClick="btnformatric_Click" />

gridview rowcommand 后面的代码如下

protected void gvDoctorList_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "Select")
        {
            int pID = Convert.ToInt32(e.CommandArgument);
            Session["PatientId"] = Convert.ToString(e.CommandArgument);
            //Server.Transfer("Patientstaticformatrix.aspx");

             string pIDstr = Convert.ToString(Session["PatientId"]);
             if (!string.IsNullOrEmpty(pIDstr))
             {
                 int patientID = Convert.ToInt32(pID);

                 //string connection = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
                 string sqlquery = "SELECT * FROM [MyDatabase].[dbo].[PatExam] where PId = '" + patientID + "'";
                 string connection = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
                 using(SqlConnection conn = new SqlConnection(connection))
                 {
                     //SqlConnection conn = new SqlConnection(con);
                     DataSet ds;
                     ds = new DataSet();
                     SqlDataAdapter cmpatientexam;
                     conn.Open();

                     cmpatientexam = new SqlDataAdapter(sqlquery, conn);
                     cmpatientexam.Fill(ds, "PatientExam");

                     TreeNode pidnode = new TreeNode();
                     pidnode.Text = pIDstr;



                     foreach (DataRow patrow in ds.Tables["PatientExam"].Rows)
                     {

                             //TreeNode tvpatexam = new TreeNode();
                             //tvpatexam.Text = patrow["PId"].ToString();
                             //TreeView1.Nodes.Add(tvpatexam);

                             //for (int i = 0; i < ds.Tables["PatientExam"].Columns["PId"].Count; i++)
                             //if (patrow["PId"].ToString() != DBNull.Value)
                             //{                               

                                 TreeNode childtvpatexam = new TreeNode();
                                 childtvpatexam.Text = patrow["Exam"].ToString();
                                 pidnode.ChildNodes.Add(childtvpatexam);
                                 //break;
                             //}

                         //TreeView1.Nodes.Add(tvpatexam);
                     }

                     TreeView1.Nodes.Add(pidnode);
                     ds.Dispose();
                     cmpatientexam.Dispose();
                     conn.Close();
                     conn.Dispose();
                 }
             }
        }


    }

按钮点击事件背后的代码如下

 protected void btnformatric_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in gvDoctorList.Rows)
        {
            Button btn = (Button)row.FindControl("Select");

            if (btn != null)
            {

                string pIDstr = Convert.ToString(Session["PatientId"]);
                string exam = ((Button)sender).Text;

                SqlCommand cmd = new SqlCommand("INSERT INTO [dbo].[PatExam]([PId],[Exam]) VALUES (@pid,@exam)", con);

                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                try
                {
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@pid", pIDstr);
                    cmd.Parameters.AddWithValue("@exam", exam);



                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    Response.Write("Error Occured: " + ex.Message.ToString());
                }
                finally
                {
                    con.Close();
                    cmd.Dispose();
                }          
            }
        }
}

我想插入使用选择按钮从gridview中选择的值,并在按钮单击事件中插入该选定值....但是使用上面的代码它不起作用...

任何人都可以建议我一些其他的想法,或者如果可能的话用这些代码那么如何......你能给我它的代码......非常感谢

4

2 回答 2

1

您可以添加一个隐藏字段并将 Gridview 的 rowindex 保存在隐藏字段中。在 btnformatric_Click 中,您可以按索引获取行并获取数据。标记:

 <asp:HiddenField ID="hdnRowIndex" runat="server" Value ="" />
 <asp:Button ID="btnformatric" runat="server" Text="formatric3d" OnClick="btnformatric_Click" />

在代码中,gvDoctorList_RowCommand 方法:

protected void gvDoctorList_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            if (e.CommandName == "Select")
            {
                int pID = Convert.ToInt32(e.CommandArgument);
                Session["PatientId"] = Convert.ToString(e.CommandArgument);
                //Server.Transfer("Patientstaticformatrix.aspx");
                GridViewRow gvr = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
                hdnRowIndex.Value = gvr.RowIndex.ToString();
... ... ...     ... ... ...     ... ... ...     ... ... ...     ... ... ... 

btnformatric_Click 方法:

protected void btnformatric_Click(object sender, EventArgs e)
{
    int rowIndex = 0;
    if (int.TryParse(hdnRowIndex.Value, out rowIndex))
    {
        //Get the row
        GridViewRow row = gvDoctorList.Rows[rowIndex];            
        Button btn = (Button)row.FindControl("Select");

        if (btn != null)
        {

            string pIDstr = Convert.ToString(Session["PatientId"]);
            string exam = ((Button)sender).Text;

            SqlCommand cmd = new SqlCommand("INSERT INTO [dbo].[PatExam]([PId],[Exam]) VALUES (@pid,@exam)", con);

            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            try
            {
                cmd.Connection = con;
                cmd.Parameters.AddWithValue("@pid", pIDstr);
                cmd.Parameters.AddWithValue("@exam", exam);

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Response.Write("Error Occured: " + ex.Message.ToString());
            }
            finally
            {
                con.Close();
                cmd.Dispose();
            }
        }
    }
}
于 2013-10-14T15:03:21.830 回答
0

我想您有兴趣根据PatientID从 GridView 中选择的值插入记录吗?

您要做的是首先将 GridView 的DataKeyNames属性设置为PatientID

<asp:GridView ID="gvDoctorList" runat="server" DataKeyNames="PatientID" ...>

然后在btnformatric_Click事件处理程序中,您可以通过 获取所选PatientIDgvDoctorList.SelectedValue,如下所示:

string pIDstr = gvDoctorList.SelectedValue.ToString();

当然,在执行上述操作之前,您应该检查以确保用户已从网格中选择了一名患者(即 that gvDoctorList.SelectedIndex >= 0)。

于 2013-10-14T14:58:45.550 回答