Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Star
10672 Points
2426 Posts
May 01, 2012 02:41 PM|LINK
Try this sample. But I am notsure why do you want to make the cell editable.
ListBox and GridView
<asp:ListBox ID="ListBox1" runat="server"> <asp:ListItem>Manager1</asp:ListItem> <asp:ListItem>Manager2</asp:ListItem> <asp:ListItem>Manager3</asp:ListItem> <asp:ListItem>Manager4</asp:ListItem> </asp:ListBox> <asp:GridView ID="UserAllocationGrid" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="Manager" HeaderText="Manager" SortExpression="managers" /> <asp:TemplateField HeaderText="Allocation Percentage"> <ItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text= '<%# Bind("AllocationPercentage") %>' BorderStyle="None"></asp:TextBox> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
Code Behind
void fillGV() { DataTable UserAllocationTable = new DataTable(); UserAllocationTable.Columns.Add("Manager"); UserAllocationTable.Columns.Add("AllocationPercentage"); // go through listbox1 to find selected managers = selectedManagersList List<string> selectedManagersListDates = new List<string>(); int counterR = 0; foreach (ListItem strItem in ListBox1.Items) { //selectedManagersListDates.Add(strItem.Value); DataRow drManagerName = UserAllocationTable.NewRow(); UserAllocationTable.Rows.Add(drManagerName); UserAllocationTable.Rows[counterR]["Manager"] = strItem.Value; counterR = counterR + 1; } // ViewState["UserAllocationTable"] = UserAllocationTable; UserAllocationGrid.DataSource = UserAllocationTable; UserAllocationGrid.DataBind(); }
Use this void in any event I did it in abutton click
protected void Button1_Click(object sender, EventArgs e) { fillGV(); }
Please note the changes I made to your code
basheerkal
Star
10672 Points
2426 Posts
Re: Make 1 column in gridview editable for user
May 01, 2012 02:41 PM|LINK
Try this sample. But I am notsure why do you want to make the cell editable.
ListBox and GridView
<asp:ListBox ID="ListBox1" runat="server"> <asp:ListItem>Manager1</asp:ListItem> <asp:ListItem>Manager2</asp:ListItem> <asp:ListItem>Manager3</asp:ListItem> <asp:ListItem>Manager4</asp:ListItem> </asp:ListBox> <asp:GridView ID="UserAllocationGrid" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="Manager" HeaderText="Manager" SortExpression="managers" /> <asp:TemplateField HeaderText="Allocation Percentage"> <ItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text= '<%# Bind("AllocationPercentage") %>' BorderStyle="None"></asp:TextBox> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>Code Behind
void fillGV() { DataTable UserAllocationTable = new DataTable(); UserAllocationTable.Columns.Add("Manager"); UserAllocationTable.Columns.Add("AllocationPercentage"); // go through listbox1 to find selected managers = selectedManagersList List<string> selectedManagersListDates = new List<string>(); int counterR = 0; foreach (ListItem strItem in ListBox1.Items) { //selectedManagersListDates.Add(strItem.Value); DataRow drManagerName = UserAllocationTable.NewRow(); UserAllocationTable.Rows.Add(drManagerName); UserAllocationTable.Rows[counterR]["Manager"] = strItem.Value; counterR = counterR + 1; } // ViewState["UserAllocationTable"] = UserAllocationTable; UserAllocationGrid.DataSource = UserAllocationTable; UserAllocationGrid.DataBind(); }Use this void in any event I did it in abutton click
protected void Button1_Click(object sender, EventArgs e) { fillGV(); }Please note the changes I made to your code
(Talk less..Work more)