如何在动态添加的gridview控件中检索和绑定数据库中的值?

时间:2013-10-22 07:39:52

标签: c# asp.net gridview datatable

我想从gridview

中检索和绑定数据库中的值

以下是在gridview中动态添加控件的代码,此代码工作正常

代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        SetInitialRow();
    }
}

private void SetInitialRow()
{
    DataTable dt = new DataTable();
    DataRow dr = null;
    dt.Columns.Add(new DataColumn("Column0", typeof(string)));
    dt.Columns.Add(new DataColumn("Column1", typeof(string)));
    dt.Columns.Add(new DataColumn("Column2", typeof(string)));
    dt.Columns.Add(new DataColumn("Column3", typeof(string)));
    dt.Columns.Add(new DataColumn("Column4", typeof(string)));
    dt.Columns.Add(new DataColumn("Column5", typeof(string)));
    dt.Columns.Add(new DataColumn("Column6", typeof(string)));
    dt.Columns.Add(new DataColumn("Column7", typeof(string)));
    dr = dt.NewRow();

    dr["Column0"] = string.Empty;
    dr["Column1"] = string.Empty;
    dr["Column2"] = string.Empty;
    dr["Column3"] = string.Empty;
    dr["Column4"] = string.Empty;
    dr["Column5"] = string.Empty;
    dr["Column6"] = string.Empty;
    dr["Column7"] = string.Empty;
    dt.Rows.Add(dr);
    dr = dt.NewRow();

    //Store the DataTable in ViewState
    ViewState["CurrentTable"] = dt;
    Gridview1.DataSource = ViewState["CurrentTable"];
    Gridview1.DataSource = dt;
    Gridview1.DataBind();
}

以下是在gridview中添加一行的代码

private void AddNewRowToGrid()
{
    int rowIndex = 0;

    if (ViewState["CurrentTable"] != null)
    {
        DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
        DataTable dtCurrentTable1 = (DataTable)Application["CurrentTable"];
        DataRow drCurrentRow = null;
        if (dtCurrentTable.Rows.Count > 0)
        {
            for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
            {
                //extract the TextBox values
                DropDownList drop1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[0].FindControl("dropActivities");
                TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
                TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
                TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox5");
                TextBox box6 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox6");
                TextBox box7 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl("TextBox7");
                drCurrentRow = dtCurrentTable.NewRow();

                dtCurrentTable.Rows[i - 1]["Column0"] = drop1.SelectedItem.Text;
                dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
                dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
                dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;
                dtCurrentTable.Rows[i - 1]["Column4"] = box4.Text;
                dtCurrentTable.Rows[i - 1]["Column5"] = box5.Text;
                dtCurrentTable.Rows[i - 1]["Column6"] = box6.Text;
                dtCurrentTable.Rows[i - 1]["Column7"] = box7.Text;

                rowIndex++;
            }

            dtCurrentTable.Rows.Add(drCurrentRow);

            Application["CurrentTable"] = dtCurrentTable;
            Gridview1.DataSource = dtCurrentTable;
            Gridview1.DataBind();
        }
    }
    else
    {
        Response.Write("ViewState is null");
    }

    //Set Previous Data on Postbacks
    SetPreviousData();
}

保留上一行值的代码

private void SetPreviousData()
{
    int rowIndex = 0;
    if (ViewState["CurrentTable"] != null)
    {
        DataTable dt = (DataTable)ViewState["CurrentTable"];
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DropDownList drop1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[0].FindControl("dropActivities");
                TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
                TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
                TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox5");
                TextBox box6 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox6");
                TextBox box7 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl("TextBox7");

                drop1.SelectedValue = dt.Rows[i]["Column0"].ToString();
                box1.Text = dt.Rows[i]["Column1"].ToString();
                box2.Text = dt.Rows[i]["Column2"].ToString();
                box3.Text = dt.Rows[i]["Column3"].ToString();
                box4.Text = dt.Rows[i]["Column4"].ToString();
                box5.Text = dt.Rows[i]["Column5"].ToString();
                box6.Text = dt.Rows[i]["Column6"].ToString();
                box7.Text = dt.Rows[i]["Column7"].ToString();
                rowIndex++;
            }
        }
    }
}

数据库中保存值的代码:

protected void btnsave_Click(object sender, EventArgs e)
{
    int rowIndex = 0;
    StringCollection sc = new StringCollection();
    if (ViewState["CurrentTable"] != null)
    {
        DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
        if (dtCurrentTable.Rows.Count > 0)
        {
            for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
            {
                //extract the TextBox values
                DropDownList drop1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[0].FindControl("dropActivities");
                TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
                TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox4");
                TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox5");
                TextBox box6 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox6");
                TextBox box7 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox7");

                DbConnection.Open();
                OleDbCommand cmd1 = new OleDbCommand("select emp_id from emp_master where username = '" + username + "'", DbConnection);
                string empid = (string)cmd1.ExecuteScalar();

                OleDbCommand cmd = new OleDbCommand("INSERT INTO activity_values (id,emp_id,activities,monday,tuesday,wednesday,thursday,friday,saturday,sunday,week) VALUES(id.nextval,'" + empid + "','" + drop1.SelectedItem.Text + "', '" + box1.Text + "', '" + box2.Text + "', '" + box3.Text + "', '" + box4.Text + "', '" + box5.Text + "', '" + box6.Text + "', '" + box7.Text + "','" + weekNumNow + "')", DbConnection);
                cmd.ExecuteNonQuery();
                DbConnection.Close();
                sc.Add(drop1.SelectedItem.Text +"," + box1.Text + "," + box2.Text + "," + box3.Text + "," + box4.Text + "," + box5.Text + "," + box6.Text + "," + box7.Text);
                rowIndex++;
            }
            //Call the method for executing inserts
            //InsertRecords(sc);
        }
    }

表格

  Activities    Monday Tues  Wedn Thu  Fri   Sat   Sun    Week  Id
  Permission     1      5    3     4   5     6     5      43    1
  Fun @ work     2      5    4     4   6     7     4      43    2

检索值的代码并将其绑定在gridview中

但是这段代码无效

public void BindData()
{ 
    int rowIndex = 0;
    DataRow drCurrentRow;
    OleDbCommand cmd = new OleDbCommand("select activities,monday,tuesday,wednesday,thursday,friday,saturday,sunday from activity_values where week = '" + weekNumNow + "' and emp_id = (select emp_id from emp_master where username = '" + username + "')", DbConnection);
    DbConnection.Open();
    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
    DataTable ds = new DataTable();
    da.Fill(ds);
    ViewState["CurrentTable"] = ds;
    Gridview1.DataSource = ds;
    Gridview1.DataBind();
    if (ViewState["CurrentTable"] != null)
    {
        DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];

        if (dtCurrentTable.Rows.Count > 0)
        {
            for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
            {
                //extract the TextBox values
                DropDownList drop1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[0].FindControl("dropActivities");
                TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
                TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
                TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox5");
                TextBox box6 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox6");
                TextBox box7 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl("TextBox7");
                drCurrentRow = dtCurrentTable.NewRow();

                //

                rowIndex++;
            }

            dtCurrentTable.Rows.Add(drCurrentRow);
        }
    }
    rowIndex++;
}

enter image description here

有什么想法吗?提前致谢

0 个答案:

没有答案