Laden...

Boundfield von Gridview auslesen und auf read only setzen

Erstellt von King-Malkav vor 16 Jahren Letzter Beitrag vor 16 Jahren 2.752 Views
King-Malkav Themenstarter:in
264 Beiträge seit 2006
vor 16 Jahren
Boundfield von Gridview auslesen und auf read only setzen

Hallo,

ich habe ein kleines Problem mit meinem "Lieblings" Element dem Gridview.

Ich habe ein BoundField das einen Termin hat. Wenn ein Termin eingetragen ist in der DB, soll er das Feld auf Read Only setzen, so das im Edit Modus das Datum nicht mehr geändert werden kann.

Sollte das Datum leer sein, dann muss Read Only auf false gesetzt werden.

Hier mal noch ein wenig Code...

Mein Gridview


<asp:GridView ID="gv_todo" runat="server" CellPadding="4"
                        ForeColor="#333333" 
                        GridLines="None" AutoGenerateColumns="False" 
                        DataKeyNames="IDNR" 
                        EmptyDataText="Es gibt keine Meilensteine für dieses Projekt..." 
                        Width="620px" 
                        OnRowUpdating="gv_todo_RowUpdating" OnRowDataBound="gv_todo_RowDataBound" 
                        OnRowEditing="gv_todo_RowEditing1" OnRowCancelingEdit="gv_todo_RowCancelingEdit">
                        
                        
                        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <RowStyle BackColor="#F7F6F3" CssClass="text2" ForeColor="#333333" />
                        <EditRowStyle BackColor="Yellow" />
                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                        <HeaderStyle BackColor="Gray" CssClass="text2" Font-Bold="True" ForeColor="White" />
                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                        
                        
                        <Columns>
                            <asp:TemplateField HeaderText="sdfsd" Visible="False">
                                <ItemTemplate>
                                    <asp:TextBox ID="tb_datum" Text='<%# Eval("Datum") %>' Visible="false" runat="server"></asp:TextBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                             
                             <asp:TemplateField HeaderText="dfsd" Visible="False">
                                <ItemTemplate>
                                    <asp:CheckBox ID="cb_ok" Text='<%# Eval("OK") %>' Visible="false" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            
                            <asp:BoundField DataField="Datum" DataFormatString="{0:d}"  HeaderText="Termin" HtmlEncode="False" ApplyFormatInEditMode="True" />
                            <asp:BoundField DataField="Meilenstein" HeaderText="Meilenstein" />
                            <asp:CheckBoxField DataField="OK" HeaderText="Erledigt" />
                            <asp:BoundField DataField="Datumerledigt" HeaderText="Datum" ReadOnly="true" />
                            
                            
                            <asp:TemplateField>
                                
                                
                                <ItemTemplate>
                                    <asp:Button ID="Button4" CommandName="Edit" Text="Edit" Font-Size="8pt" Width="45px"
                                        runat="Server" />
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:Button ID="Button5" CommandName="Update" Text="Update" Font-Size="8pt" Width="45px"
                                        runat="Server" />
                                    <asp:Button ID="Button6" CommandName="Cancel" Text="Cancel" Font-Size="8pt" Width="45px"
                                        runat="Server" />
                                </EditItemTemplate>
                                <ItemStyle VerticalAlign="Top" />
                            </asp:TemplateField>
                        </Columns>
                        
                        
                        <EmptyDataRowStyle CssClass="text2" />
                        
                    </asp:GridView>

Hier noch meine Prüfen Funktion...


protected void gv_todo_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TextBox datum = e.Row.Cells[0].FindControl("tb_datum") as System.Web.UI.WebControls.TextBox;

            if (datum.Text != string.Empty)
            {
                foreach (TableCell c in e.Row.Cells)
                {
                    e.Row.Cells[2].Enabled = false;
                }
            }

            CheckBox cb = e.Row.Cells[1].FindControl("cb_ok") as System.Web.UI.WebControls.CheckBox;

            if (cb.Text == "True")
            {
                foreach (TableCell c in e.Row.Cells)
                {
                    c.BackColor = System.Drawing.Color.GreenYellow;
                    e.Row.Cells[6].Enabled = false;
                }
            }
        }
    }

Allerdings gelingt es mir nicht, das BoundField Datum auf ReadOnly zu setzen.

Für Hilfe wäre ich sehr Dankbar.

MFG Daniel

King-Malkav Themenstarter:in
264 Beiträge seit 2006
vor 16 Jahren

Keiner ne Idee?

5.942 Beiträge seit 2005
vor 16 Jahren

Hallo Daniel

Du setzt bei dir nur die Tabellenzellen auf disabled.
Das endet dann in: <td disabled="disabled">bla</td>, was nichts bewirkt.
In meinem Beispiel checke ich das der RowState auf edit steht und greife auf die TextBox zu, die beim editieren erstellt wird.
Cells[1] desshalb, weil eine Spalte für die Editierlinks benutzt wird.


    void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow
            &&
            e.Row.RowState == DataControlRowState.Edit
            )
        {
            if (DataBinder.Eval(e.Row.DataItem, "fld_xyz").ToString() != String.Empty)
            {
                TextBox t = (TextBox)e.Row.Cells[1].Controls[0];
                t.Enabled = false;
            }
        }
    }

HTH

Gruss Peter

--
Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland 2007 - 2011