2012年4月20日 星期五

GridView不經過資料庫就能增刪改

實際上是這個,看了之後想要把程式碼下載下來直接用...
...然後就是,他的程式碼不見了,所以我重新做出來(簡潔)
因為拉控件很麻煩我還少一個欄位,所以程式長得更迷你了呢~(這樣好嗎?)


直接進程式碼!
<table>
   <tr><td>時間</td><td>數量</td></tr>
   <tr>
  <td>    
   <asp:TextBox ID="txtPoDate" runat="server" Width="100"></asp:TextBox>
  </td>
  <td>
   <asp:TextBox ID="tbxQTY" runat="server"></asp:TextBox>
  </td>
  <td>
     <asp:Button ID="btnInsert" runat="server" Text="新增"
      onclick="btnInsert_Click" />
  </td>
 </tr>
</table>
<asp:GridView ID="gv_Data" runat="server" AutoGenerateColumns="False">
     <Columns>
  <asp:BoundField DataField="id" HeaderText="順序" />
  <asp:BoundField DataField="DateTime" HeaderText="日期" />
  <asp:BoundField DataField="QTY" HeaderText="數量" />
  <asp:TemplateField>
  <ItemTemplate>
   <asp:LinkButton ID="lbtnEdit" runat="server"
       CommandArgument='<%#Eval("id")%>'
       OnClick="lbtnEdit_Click">修改</asp:LinkButton>
   <asp:LinkButton ID="lbtnDelete" runat="server"
       CommandArgument='<%#Eval("id")%>'
       OnClick="lbtnDelete_Click"
        OnClientClick="return confirm('確定刪了喔?')">刪除</asp:LinkButton>
  </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

就是簡單的兩個Textbox跟一個按鈕,下面一個Gridview,要不要預先塞資料進去都可以啦
後台長這樣,基本上用2010的話,就是上下各留兩個大括號,Page_Load也可以覆寫掉

        DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {              
                 //覺得一開始沒有假資料就好像出門沒擠事業線或是沒化妝的可以把這段註解解開
                 //dt.Columns.Add("id");
                //dt.Columns.Add("DateTime");
                //dt.Columns.Add("QTY");
                //dt.Rows.Add(new object[] { "100", "20110101", "100" });
                //dt.Rows.Add(new object[] { "101", "20110102", "100" });
                //gv_Data.DataSource = dt;
                //gv_Data.DataBind();
            }
        }
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            dt.Columns.Add("id");
            dt.Columns.Add("DateTime");
            dt.Columns.Add("QTY");
            for (int i = 0; i < gv_Data.Rows.Count; i++)
            {
                //CommandArgument不為空就是修改狀態
                //增加index判斷
                if (btnInsert.CommandArgument != string.Empty)
                {
                    if (i + 1 == int.Parse(btnInsert.CommandArgument))
                    {
                        //若index相同就修改資料
                        dt.Rows.Add(new object[] { i + 1, txtPoDate.Text.Trim(), tbxQTY.Text.Trim() });
                    }
                    else
                    {
                        //回填Gridview原有資料
                        dt.Rows.Add(new object[] { i + 1,
                                                gv_Data.Rows[i].Cells[1].Text.Replace("&nbsp;",""),
                                                gv_Data.Rows[i].Cells[2].Text.Replace("&nbsp;","")});
                    }
                }
                else
                {
                    //回填Gridview原有資料
                    dt.Rows.Add(new object[] { i + 1,
                                                gv_Data.Rows[i].Cells[1].Text.Replace("&nbsp;",""),
                                                gv_Data.Rows[i].Cells[2].Text.Replace("&nbsp;","")
                    });
                }            
            }
            if (btnInsert.Text == "新增")
            {
                dt.Rows.Add(new object[] { dt.Rows.Count + 1, txtPoDate.Text.Trim(), tbxQTY.Text.Trim() });
            }
            //清空資料
            btnInsert.CommandArgument = string.Empty;
            btnInsert.Text = "新增";
            //繫結
            gv_Data.DataSource = dt;
            gv_Data.DataBind();
            //回預設
            txtPoDate.Text = string.Empty;
            tbxQTY.Text = string.Empty;
        }
        protected void lbtnEdit_Click(object sender, EventArgs e)
        {
            //CommandArgument為id( -1為index)
            LinkButton lbtn = (LinkButton)sender;
            GridViewRow gvr = gv_Data.Rows[int.Parse(lbtn.CommandArgument) - 1];
            //資料填入上方空格
            txtPoDate.Text = gvr.Cells[1].Text;
            tbxQTY.Text = gvr.Cells[2].Text;
            btnInsert.CommandArgument = lbtn.CommandArgument;
            btnInsert.Text = "修改";
        }
        protected void lbtnDelete_Click(object sender, EventArgs e)
        {
            dt.Columns.Add("id");
            dt.Columns.Add("DateTime");
            dt.Columns.Add("QTY");
            int iRow = 0;
            for (int i = 0; i < gv_Data.Rows.Count; i++)
            {
                //為刪除的index就跳過這列不回填
                if (i + 1 != int.Parse(((LinkButton)sender).CommandArgument))
                {
                    //回填Gridview原有資料
                    dt.Rows.Add(new object[] { i + 1,
                                                gv_Data.Rows[i].Cells[1].Text.Replace("&nbsp;",""),
                                                gv_Data.Rows[i].Cells[2].Text.Replace("&nbsp;","")
                    });
                    iRow ++;
                }
            }
            //繫結
           gv_Data.DataSource = dt;
           gv_Data.DataBind();
        }

沒有留言:

張貼留言