从另一个gridview的一组值中填充gridvview并将其保存到SQL数据库中

时间:2014-12-08 20:11:08

标签: c# asp.net sql-server visual-studio-2013

我有一个带有两个步骤的向导,第一个我有一个带有附加SQL数据源的Gridview1,显示一个列和一个模板字段,在模板字段中有一个复选框,我用它来选择一行gridview1,因此可以获得一组选定的行。我希望只有那些选定的值(行)出现在第二个gridview中,没有sqldatasource,只有一个带有头文本的列,我设法做到这一点,但后来我想将这个gridview保存在我的SQL中的空表中服务器数据库有2列,我只传递一个值(名称),因为ID是自动递增的,我没有收到任何错误,但数据库没有正确更新。我认为问题在于Addwithvalue参数。这是代码,提前谢谢。

<form id="form1" runat="server">
<div>
    <asp:Wizard ID="Wizard1" runat="server" OnNextButtonClick="Wizard1_NextButtonClick" ActiveStepIndex="0">  
        <WizardSteps>
            <asp:WizardStep runat="server" title="Step 1">
                 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" ShowFooter="True" >
                     <%--Gridview with it assigned sql Data-source--%>
        <Columns> <%--Between this tags goes the columns of the gridview--%>
            <asp:TemplateField HeaderText="Task_temp_name" SortExpression="Task_temp_name">
                <EditItemTemplate> <%--Edit template is what will appear when you are editing the that field of the gridview--%>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Task_temp_name") %>'></asp:TextBox> <%--in this case a texbox which will edit the info in the column Task_tempname--%>
                </EditItemTemplate>
                <ItemTemplate> <%--Item template is the value which is displayed in the field of the gridview--%>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Task_temp_name") %>'></asp:Label> <%--a label in this case--%>
                </ItemTemplate>
                  <FooterTemplate> <%--We use this footer template to insert data into the gridview by writting in a texbox and clicking a button--%>
                          <asp:LinkButton ID="CreateButton" runat="server" OnClick="CreateButton_Click">Add New Task</asp:LinkButton> <%--this button adds what we write on the texbox to a new row in the DB--%>
                          <asp:TextBox ID="AddText" runat="server"></asp:TextBox>  <%--the button has his logic in the c# side in the event onclick= CreateButton_Click--%>
                  </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <EditItemTemplate> <%--the field for the checkbox to select the items we want--%>
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
                <%--Here we see the SqlDatasource which is attached to the previous gridview--%>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"  
        SelectCommand="SELECT [Task_temp_name] FROM [Task_templates]" OldValuesParameterFormatString="original_{0}">
    </asp:SqlDataSource>

            </asp:WizardStep>
            <asp:WizardStep runat="server" title="Step 2">
                 <%--In this gridview we have no Datasource because we want it to be populated according to the values selected on gridview1--%>
        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="True">
            <Columns>

            </Columns>


    </asp:GridView>

            </asp:WizardStep>
        </WizardSteps>
    </asp:Wizard>
   <br />

</div>
</form>

`

// C#HERE

protected void Page_Load(object sender, EventArgs e)
{

    LinkButton AddButton = new LinkButton(); //Creation of the button on the footer to add new task_Templates YOU CAN IGNORE THAT
    AddButton.Click += new EventHandler(CreateButton_Click); //creation how the eventhandler to handle the behaviour of that button

}
protected void CreateButton_Click(object sender, EventArgs e) //event handler where the text inside the texbox is added as a new row in the database
{

    SqlDataSource1.InsertParameters["Task_temp_name"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("AddText")).Text;
    SqlDataSource1.Insert();
}

protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e) //eventhandler for the next button click on the wizard when we want to retrieve the selected values
//and store them into the database
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[1] { new DataColumn("Task_name") });
    foreach (GridViewRow row in GridView1.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chkRow = (row.Cells[1].FindControl("CheckBox1") as CheckBox);
            if (chkRow.Checked)
            {

                string task_temp_name = (row.Cells[0].FindControl("Label1") as Label).Text;

                dt.Rows.Add(task_temp_name);
            }
        }
    }

    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
    {

        con.Open();
        GridView2.DataSource = dt;
        GridView2.DataBind(); // I THINK EVERYTHING IS FINE UNTIL HERE



        string Task_name = string.Empty;
        foreach (GridViewRow GVRow in GridView1.Rows)
        {
            Task_name = GVRow.Cells[1].Text;

            string sqlcmd = @"insert into Tasks (Task_name) values (@Task_temp_name)";


            SqlCommand cmd = new SqlCommand(sqlcmd, con);


            cmd.CommandText = sqlcmd;
            cmd.Parameters.AddWithValue("@Task_temp_name", Task_name.ToString());
            cmd.ExecuteNonQuery();

        }

    }
}

0 个答案:

没有答案