簡體   English   中英

如何動態地將數據添加到datatable(Gridview)

[英]how to add data into datatable(Gridview) dynamically

我想如何將數據添加到數據表中並更改“日期”列的dataformatString 在此處輸入圖片說明

Q1。 如何將數據格式更改為“ dd / MM / yyyy”或使用您的方法顯示
Q2。 我已經在page_load事件中添加了用於添加數據鍵的代碼:[“ RowID”],但是當我單擊“編輯”按鈕時,它提示“對象引用未設置為對象的實例”,如何獲取數據鍵值/添加commandArguement來“編輯” /“刪除”按鈕
Q3。 當我單擊“編輯”按鈕時,如何通過更新,取消按鈕等進入編輯模式進行編輯,

我希望有人能幫助我,非常感謝!!!

備注:“編輯和刪除按鈕不是動態添加在.aspx中”

的.cs

protected void Page_Load(object sender, EventArgs e){
if (!IsPostBack) {
        GridView gv = (GridView)Page.FindControl("GridView1");
        gv.DataKeyNames = new string[] { "RowID" };
    }
}

protected void btn_Click(object sender, EventArgs e){
  getDT(date);  //string []date;
}

private DataTable getDT(string[] date){
    DataTable dt = new DataTable();

    dt.Columns.Add("RowID", typeof(Int16));
    dt.Columns.Add("Date", typeof(DateTime));

    for (int i = 0; i < date.Length; i++) {
        dt.Rows.Add(i + 1, date[i]); //date[i] format:yyyy-mm-dd or dd/MM/yyyy
    } 
    return dt;
}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {
    if (e.CommandName == "Delete"){
       hf_id.Value = GridView1.DataKeys[0].Value.ToString();
    }
}

從給定的鏈接下載源代碼並進行研究會對您有所幫助。 您的要求很簡單,但是必須具備基本知識。

http://www.codeproject.com/Articles/23471/Editable-GridView-in-ASP-NET

這是正確的..但是我想在不使用數據庫的情況下成為這個。.我做了一切..現在我想要如果我單擊單元格,以便數據在gv中更新,但是它沒有顯示datatable..so,我想刷新按鈕..如果我單擊此按鈕,則應在數據表上更新數據..我正在發送這樣的代碼...

設計頁面

網格視圖

碼 - -

    protected void Page_Load(object sender, EventArgs e){
        if (!this.IsPostBack) {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("City") });

            dt.Rows.Add(1, "Anamika", "Bangalore");
            dt.Rows.Add(2, "Sunny", "Chennai");
            dt.Rows.Add(3, "Monika", "Bangalore");
            dt.Rows.Add(4, "Jyoti", "Chennai");
            dt.Rows.Add(5, "Radhika", "Jabalpur");
            dt.Rows.Add(6, "Imran", "Jammu");
            dt.Rows.Add(7, "Alok", "Delhi");
            dt.Rows.Add(8, "Amit", "Shamshabad");
            dt.Rows.Add(9, "Neetu", "Bhopal");
            dt.Rows.Add(10, "Jyoti", "Chennai");
            dt.Rows.Add(11, "Radhika", "Vidisha");
            dt.Rows.Add(10, "Pooja", "Pune");

            gridview1.DataSource = dt;
            gridview1.DataBind();
        }
    } 

    protected void gridview1_OnRowDataBound(object sender, GridViewRowEventArgs e) {
        if (e.Row.RowType == DataControlRowType.DataRow) {
            for (int i = 0; i < e.Row.Cells.Count; i++) {                 
                TextBox txt = new TextBox();
                txt.Text = e.Row.Cells[i].Text;                 
                e.Row.Cells[i].Text = "";
                e.Row.Cells[i].Controls.Add(txt);
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM