填充GridView,动态模板化编辑字段,保存到数据库

时间:2016-08-08 18:54:16

标签: c# asp.net gridview webforms datetimepicker

好吧,我会尝试尽可能彻底。如果有人想知道/ =

,我已经经历过几个(很多)教程,然后才能在这里发帖

我有几个表 - 一个包含text / stored-procedure-name对,它们将填充前端的下拉菜单(Web窗体)。其他表通过存储过程访问。我的想法是,我希望能够从后端控制哪些字段可见/可编辑到前端的用户。我还希望能够添加/删除存储过程来检索数据,而不必弄乱前端。

用户可以从下拉列表中进行选择,并根据选择执行存储过程,然后填充gridview。到目前为止没问题。

现在,理想情况下,用户将能够编辑gridview中显示的数据。这很容易,直到我们开始变得有趣(即覆盖生成的默认文本框控件)。例如,当用户按下GridView行中的“edit”,并且记录的数据类型为“DateTime”时,我想要弹出某种日历/时钟并允许用户选择日期和时间,然后更新它回到数据库中。

此时我一直在遇到障碍。我已经按照关于动态生成可编辑网格视图(http://aspalliance.com/1125_Dynamically_Templated_GridView_with_Edit_Delete_and_Insert_Options.1)的教程进行了操作,但它不适用于我想要做的事情。

非常感谢任何帮助,我希望我已经足够深入地解释了这一点!

代码:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    this.setData();
}

public void setData()
{

    //Establishing the SQL Connection
    using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["myConnString"].ConnectionString
        ))
    {

        //string query;
        SqlCommand SqlCommand;
        SqlDataReader reader;

        SqlDataAdapter adapter = new SqlDataAdapter();
        //Open the connection to db
        conn.Open();

        //Generating the query to fetch the contact details
        //query = "SELECT * FROM " + DropDownList1.SelectedValue + ";";

        SqlCommand = new SqlCommand(DropDownList1.SelectedValue, conn);
        //execute the query
        reader = SqlCommand.ExecuteReader();
        //Assign the results 
        GridView1.DataSource = reader;

        //Bind the data
        GridView1.DataBind();

        conn.Close();
    } }

问题是当它被触发时:

  protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;

    this.setData();
}

所有可编辑字段都默认为文本框。我根本无法根据数据类型进行格式化。同样,作为示例,如果数据类型是DateTime,则应弹出日历并让用户选择日期/时间。

我知道有一种方法可以使用,除非这是静态生成的网格视图。此gridview是动态的,其内容取决于从后端执行的过程,因此我无法将编辑格式硬编码到前端。

0 个答案:

没有答案