关于DataGrid绑定数据后的修改
我在一个DataGrid中绑定了两个列(用户ID \ 用户姓名),一个摸版列(职务),并且填加了一个按钮列(编辑\更新\取消),希望是在点击 "编辑 "的时候,将 "职务 "这列变成一个Dropdownlist以便用户修改,但是现在的问题是:当我点击 "编辑 "的时候,虽然出现了Dropdownlist,但是之前的那两个绑定的列也自动变成了TextBox,我希望的效果是只有 "职务 "那列变成Dropdownlist,之前的列要保持原样就可以了,应该如何实现哦??
代码如下:
<asp:DataGrid id= "UserGrid " runat= "server " Width= "548px " BorderColor= "#DEDFDE " BorderStyle= "None "
BorderWidth= "1px " BackColor= "White " CellPadding= "4 " GridLines= "Vertical " ForeColor= "Black "
AutoGenerateColumns= "False " Height= "164px ">
<FooterStyle BackColor= "#CCCC99 "> </FooterStyle>
<SelectedItemStyle Font-Bold= "True " ForeColor= "White " BackColor= "#CE5D5A "> </SelectedItemStyle>
<AlternatingItemStyle BackColor= "White "> </AlternatingItemStyle>
<ItemStyle BackColor= "#F7F7DE "> </ItemStyle>
<HeaderStyle Font-Bold= "True " ForeColor= "White " BackColor= "#6B696B "> </HeaderStyle>
<Columns>
<asp:BoundColumn DataField= "uid " HeaderText= "用户ID ">
<ItemStyle Font-Size= "12pt "> </ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField= "uname " HeaderText= "用户姓名 ">
<ItemStyle Font-Size= "12pt "> </ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText= "职务 ">
<ItemTemplate>
<asp:Label id=positionLabel runat= "server " Text= ' <%# DataBinder.Eval(Container, "DataItem.dname ") %> '>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID= "position " runat= "server " Width= "100% " AutoPostBack= "True "> </asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType= "LinkButton " UpdateText= "更新 " CancelText= "取消 " EditText= "编辑 "> </asp:EditCommandColumn>
</Columns>
<PagerStyle HorizontalAlign= "Right " ForeColor= "Black " BackColor= "#F7F7DE " Mode= "NumericPages "> </PagerStyle>
</asp:DataGrid>
====================================================================================================================================================
我觉得最主要的是列的那几行
<Columns>
<asp:BoundColumn DataField= "uid " HeaderText= "用户ID ">
<ItemStyle Font-Size= "12pt "> </ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField= "uname " HeaderText= "用户姓名 ">
<ItemStyle Font-Size= "12pt "> </ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText= "职务 ">
<ItemTemplate>
<asp:Label id=positionLabel runat= "server " Text= ' <%# DataBinder.Eval(Container, "DataItem.dname ") %> '>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID= "position " runat= "server " Width= "100% " AutoPostBack= "True "> </asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType= "LinkButton " UpdateText= "更新 " CancelText= "取消 " EditText= "编辑 "> </asp:EditCommandColumn>
</Columns>
===================================================================================================================================================
<EditItemTemplate> 里我只加了 <asp:DropDownList ID= "position " runat= "server " Width= "100% " AutoPostBack= "True "> </asp:DropDownList>
但是之前的绑定列仍然自动变成了TextBox,希望高手指点一下.
顺便问下,在Grid中的DataGrid应该如何从数据库中绑定数据哦?
[解决办法]
加个 ReadOnly= "True "
<asp:BoundColumn DataField= "uid " HeaderText= "用户ID " ReadOnly= "True ">
<ItemStyle Font-Size= "12pt "> </ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField= "uname " HeaderText= "用户姓名 " ReadOnly= "True ">
<ItemStyle Font-Size= "12pt "> </ItemStyle>
</asp:BoundColumn>
[解决办法]
剩下两列如果是绑定列,让他们ReadOnly = true即可
如果是模板列,就绑定数据或使用标签即可