Gridview动态显示隐藏列

 

 1.

  <asp:TemplateField HeaderText="資料維護">
          <ItemTemplate>
            <table style="border:0px">
                 <tr>
                      <td style="border:0px">
                      <asp:Panel ID="panel1" runat="server">
                                        <a class="highslide" onclick="return rForm(this, 500, 300)"
                                            href='Rep_RepairPlan_ByempEdit.aspx?pk=<%# DataBinder.Eval(Container.DataItem,"SN") %>'>                                  
                                            維修計畫維護&nbsp;
                                            </a>
                                            </asp:Panel>
                                           </td>
                                           <td style="border:0px">
                                            <asp:label ID="lt2"  CssClass="lt" runat="server" Text=""></asp:label>
                                            <asp:Button ID="Btn_affirm" runat="server" Text="確認" Width="60px" CommandName="confirm"  />
                                           </td>
                                           <td style="border:0px">
                                              <asp:Panel ID="panel2" runat="server">
                                             <a class="highslide" onclick="return rForm(this, 700, 700)"
                                            href='REP_Record_edit2.aspx?SN=<%# DataBinder.Eval(Container.DataItem,"SN") %>'>                                  
                                            維修記錄&nbsp;
                                            </a>
                                            </asp:Panel>
                                           </td>   
                                        </tr>
                                    </table>
                                </ItemTemplate>       
                                <%--<ItemStyle Width="23%" />--%>
        </asp:TemplateField>     

 

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                string SN = this.GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
                string sql = "select C_flag from REP_date where SN='" +SN+ "'";
                string C_flag = MySqlBase.GetVarFromSql(sql, "C_flag");
                if (C_flag == "Y")
                {
                    ((Panel)e.Row.FindControl("panel1")).Visible = false;
                    ((Button)e.Row.FindControl("Btn_affirm")).Visible = false;
                    ((Panel)e.Row.FindControl("panel2")).Visible = true;
                    ((Label)e.Row.FindControl("lt2")).Visible = true;
                    ((Label)e.Row.FindControl("lt2")).Text = "已確認";
                }
                else
                {
                    ((Panel)e.Row.FindControl("panel1")).Visible = true;
                    ((Button)e.Row.FindControl("Btn_affirm")).Visible = true;
                    ((Panel)e.Row.FindControl("panel2")).Visible = false;
                    ((Button)e.Row.FindControl("Btn_affirm")).Attributes.Add("onclick", "javascript:confirm('您確認要確定嗎?')");
                    ((Label)e.Row.FindControl("lt2")).Visible = false;
                }
                e.Row.Attributes.Add("onmouseover", "onmOer(this)");
                e.Row.Attributes.Add("onmouseout", "onmOut(this)");
            }
        }
        if ((e.Row.RowType == DataControlRowType.DataRow) || (e.Row.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.Footer))
        {
            string loginUid=this.getUserId();
            if (loginUid != "admin")
            {
                e.Row.Cells[7].Visible = false;
            }
            
        }

        Button btn1 = (Button)e.Row.FindControl("Btn_affirm");
        if (btn1 != null)
        {
            btn1.CommandArgument = e.Row.RowIndex.ToString();
        }
    }

 

2.

      private void BindView(string keyword,string strCflag1,string strgp1)
    {
        SqlParameter[] p = new SqlParameter[2];
        p[0] = new SqlParameter("@keyword", keyword);
        p[1] = new SqlParameter("@strCflag", strCflag);
        DataSet dsatpc = MySqlBase.GetVarsAsDataSet("hrms_HR_ATTpatc_qry", p);
        this.GridView_patc.DataSource = dsatpc.Tables[0];
        this.GridView_patc.DataBind();
        int nStart = 0;
        this.DataViewPaginationForQuery(GridView_patc, Literal1,dsatpc,10, out nStart,keyword+"&qWord2="+strCflag1+"&qWord3="+strgp1,  this.Pager_isNewQry);
        if (rBtny.Checked)//已執行
        {
            this.GridView_patc.Columns[10].Visible = false;
        }
        if (rBtnw.Checked)//未執行
        {
            this.GridView_patc.Columns[10].Visible = true;
        }
    }

posted @ 2008-09-18 10:17  XGU_Winner  阅读(1539)  评论(0编辑  收藏  举报