为未知的Gridview数据源添加所有行复选框

时间:2011-03-23 22:48:09

标签: c# .net asp.net database gridview

我有Gridview这样的人。

<asp:GridView ID="GridView1" runat="server"  
         Width="16px" BackColor="White" BorderColor="#E7E7FF" 
        BorderStyle="None" BorderWidth="1px" CellPadding="3" 
        GridLines="Horizontal" Height="16px" >
        <AlternatingRowStyle BackColor="#F7F7F7" />
        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
        <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
        <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
        <sortedascendingcellstyle backcolor="#F4F4FD" />
        <sortedascendingheaderstyle backcolor="#5A4C9D" />
        <sorteddescendingcellstyle backcolor="#D8D8F0" />
        <sorteddescendingheaderstyle backcolor="#3E3277" />

<SortedAscendingCellStyle BackColor="#F4F4FD"></SortedAscendingCellStyle>

<SortedAscendingHeaderStyle BackColor="#5A4C9D"></SortedAscendingHeaderStyle>

<SortedDescendingCellStyle BackColor="#D8D8F0"></SortedDescendingCellStyle>

<SortedDescendingHeaderStyle BackColor="#3E3277"></SortedDescendingHeaderStyle>
    </asp:GridView>

以编程方式,我添加了一个数据源Gridview

protected void SendToGridview_Click(object sender, EventArgs e)
    {
        Calculate.Visible = true;
        MV_Label.Visible = true;
        RISK_Label.Visible = true;

        string strQuery;
        string ConnectionString = ConfigurationManager.ConnectionStrings["ora"].ConnectionString;

        OracleConnection myConnection = new OracleConnection(ConnectionString);

        strQuery = @"SELECT A.HESAP_NO, A.TEKLIF_NO1 || '/' || A.TEKLIF_NO2 AS TEKLIF, A.MUS_K_ISIM AS MUSTERI, 
                    B.MARKA, C.SASI_NO, C.SASI_DURUM, D.TAS_MAR, NVL(RISK_SASI(A.TEKLIF_NO1, A.TEKLIF_NO2, C.URUN_SIRA_NO, C.SIRA_NO),0) AS RISK,
                    NVL(MV_SASI(A.TEKLIF_NO1, A.TEKLIF_NO2, C.SIRA_NO, C.URUN_SIRA_NO, SYSDATE),0) AS MV
                    FROM S_TEKLIF A,  S_URUN B, S_URUN_DETAY C, KOC_KTMAR_PR D
                    WHERE A.TEKLIF_NO1 || A.TEKLIF_NO2 = B.TEKLIF_NO1 || B.TEKLIF_NO2
                    AND A.TEKLIF_NO1 || A.TEKLIF_NO2 = C.TEKLIF_NO1 || C.TEKLIF_NO2
                    AND B.SIRA_NO = C.URUN_SIRA_NO
                    AND B.DISTRIBUTOR = D.DIST_KOD
                    AND B.MARKA = D.MARKA_KOD
                    AND B.URUN_KOD = D.TAS_KOD ";

        string param = "";
        foreach (ListItem l in CheckBoxList1.Items)
        {
            if (l.Selected)
            {
                param += string.Format("'{0}'", l.Value);
                param += ",";
            }
        }
        param = param.Remove(param.Length - 1);

        strQuery = strQuery + " AND A.HESAP_NO IN (" + param + ")";

        OracleCommand myCommand = new OracleCommand(strQuery, myConnection);
        myCommand.CommandType = System.Data.CommandType.Text;
        myCommand.Connection = myConnection;

        myCommand.CommandText = strQuery;

        myConnection.Open();

        OracleDataReader dr = myCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

        GridView1.DataSource = dr;
        GridView1.DataBind();
        GridView1.Visible = true;

        myConnection.Close();
    }

这是我的Gridview外观;

enter image description here

我想要的是,我想在RISK列和MV列之后添加复选框列。

我的意思是,列似乎是 HESAP_NO TEKLIF MUSTERI MARKA SASI_NO SASI_DURUM TAS_MAR 风险(CheckBoxes) MV (复选框)。 Totaly应该 11 专栏。

我希望所有复选框都被默认选中。

我该怎么做?

我读过 this 文章,但在本文中Gridview是一个普通的数据源。我以编程方式添加。

1 个答案:

答案 0 :(得分:2)

您将不得不关闭AutoGenerated列,而是使用已定义的列:

<asp:GridView ID="GridView1" runat="server" Width="16px" BackColor="White"
     BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"  
     CellPadding="3" GridLines="Horizontal" Height="16px" AutoGenerateColumns="false">

     <!-- put your style stuff here -->

     <asp:BoundField HeaderText="Text" DataField="HESAP_NO" />
     <asp:BoundField HeaderText="Text" DataField="TEKLIF" />
     <asp:BoundField HeaderText="Text" DataField="MUSTERI" />
     <asp:BoundField HeaderText="Text" DataField="MARKA" />
     <asp:BoundField HeaderText="Text" DataField="SASI_NO" />
     <asp:BoundField HeaderText="Text" DataField="SASI_DURAM" />
     <asp:BoundField HeaderText="Text" DataField="TAS_MAR" />
     <asp:BoundField HeaderText="Text" DataField="RISK" />
     <asp:BoundField HeaderText="Text" DataField="Text" />

     <asp:CheckBoxField DataField="NameCheckBoxField1" HeaderText="NameCheckBoxField1" />

     <asp:BoundField HeaderText="Text" DataField="MV" />

     <asp:CheckBoxField DataField="NameCheckBoxField2" HeaderText="NameCheckBoxField2" />

</asp:GridView> 

您还需要更改SQL以返回CheckBoxField可以映射到的两个字段。这些字段应包含一个为所有情况设置true的位字段。这将允许数据自动绑定到CheckBoxField并默认选中。

EG:

SELECT
    -- all our fields
    'NameCheckBoxField1' = 0x1,
    'NameCheckBoxField2' = 0x1
FROM
    -- etc...