Reputation: 823
Im working on a small project and im programmatically switching text in a gridview with a control.
Here are the steps for the code:
private GridViewRow EnterEditMode(GridView GV, int RowIndex)
{
GridViewRow GVR = GV.Rows[RowIndex];
foreach (TableCell TC in GVR.Cells)
{
if (TC == GVR.Cells[2])
{
PlaceHolder PH = new PlaceHolder();
string CellContent1 = TC.Text;
DropDownList DDL = new DropDownList();
foreach (object s in Enum.GetValues(typeof(Ticket_Status)))
{
DDL.Items.Add(s.ToString());
}
DDL.SelectedValue = CellContent1;
DDL.ID = "I_Status_CB";
DDL.ViewStateMode = System.Web.UI.ViewStateMode.Enabled;
PH.ID = "PH_Status_CB";
PH.ViewStateMode = System.Web.UI.ViewStateMode.Enabled;
PH.Controls.Add(DDL);
this.Page.Controls.Add(PH);
TC.Controls.Add(PH);
}
}
}
Now here's the problem:
Im creating a controll and replacing text in a tablecell with that created control.
The problem is that i can't find a way to get a value from that created control!
I've tried every method i know of, i've tried to use FindControl() normal and recursive for a row, the entire table and the cell itself.
If you can spot the problem or if you have any thought on this problem please reply!
Friendly greetings,
-Raybbo.
EDIT:
It seems that i haven't given you enough information to solve this problem with me.
First thing i did was binding infromation from a SQL Database to declared BoundFields in the gridview.
private void GetTable()
{
var Tg = Ticket_Table;
if (sCon != null && sCon.State == ConnectionState.Open)
{
string SQL_Command = "SELECT * FROM Ticket_Table_2013";
SqlDataSource sDsc = new SqlDataSource(SQL_ConnectString, SQL_Command);
SqlDataAdapter sDap = new SqlDataAdapter(SQL_Command,sCon);
DataTable DT = new DataTable();
sDap.Fill(DT);
//This is where i'm adding extra columns for additional commands.
Tg.DataSource = DT;
Tg.DataBind();
sDap.Dispose();
sDsc.Dispose();
}
else { CONNECT(SQL_ConnectString); GetTable(); }
}
And here's the ASP.Net code:
<asp:GridView
ID="Ticket_Table"
CssClass="TableStyle"
ViewStateMode="Enabled"
runat="server"
OnRowUpdating="Ticket_Table_OnRowUpdating"
OnSelectedIndexChanged="Ticket_Table_OnSelectedIndexChanged"
AutoGenerateColumns="false"
AllowPaging="true"
>
<HeaderStyle CssClass="bgBlack02 black center"/>
<RowStyle CssClass="bgBlack01 center" />
<SelectedRowStyle CssClass="bgBlack02" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<div class="CmdImgWrap" id="CommandBox" runat="server">
<asp:ImageButton ID="I_Remove" CssClass="CmdImg" ImageUrl="style/Icons/Trashcan.png" runat="server" ToolTip="Remove entire row." OnClick="I_Remove_OnClick"/>
<asp:ImageButton ID="I_Edit" CssClass="CmdImg" ImageUrl="style/Icons/EditFile.png" runat="server" ToolTip="Enable edit mode." OnClick="I_Edit_OnClick"/>
<asp:ImageButton ID="I_Accept" CssClass="CmdImg" ImageUrl="style/Icons/Accept.png" Visible="false" runat="server" ToolTip="Accept changes." OnClick="I_Accept_OnClick"/>
<asp:ImageButton ID="I_Cancel" CssClass="CmdImg" ImageUrl="style/Icons/Cancel.png" Visible="false" runat="server" ToolTip="Disgard changed." OnClick="I_Cancel_OnClick"/>
<asp:ImageButton ID="I_Highlight" CssClass="CmdImg" ImageUrl="style/Icons/HighLight.png" runat="server" ToolTip="Highlight entire row." OnClick="I_Highlight_OnClick"/>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Ticket_ID" HeaderText="Ticket ID" SortExpression="Ticket_ID" />
<asp:BoundField DataField="Ticket_Status" HeaderText="Ticket status" SortExpression="Ticket_Status" />
<asp:BoundField DataField="Ticket_Title" HeaderText="Ticket title" SortExpression="Ticket_Title" />
<asp:BoundField DataField="Ticket_Description" HeaderText="Description" SortExpression="Ticket_Description" />
<asp:BoundField DataField="CompanyName" HeaderText="Company name" SortExpression="CompanyName" />
</Columns>
<PagerTemplate>
<asp:Button ID="Previous" runat="server" CommandName="Previous" Text="<"/>
<asp:Button ID="Next" runat="server" CommandName="Next" Text=">"/>
<asp:Button ID="Refresh" runat="server" CommandName="Refresh" Text="Refresh" />
<asp:Button ID="Update" runat="server" CommandName="Update" Text="Update" />
</PagerTemplate>
</asp:GridView>
Upvotes: 1
Views: 700
Reputation: 1300
Do not call the code before you DataBind
the gridview.
Remove the code from there and call inside RowDataBound
event
Haven't tested it might have some errors.
void GVR_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
string CellContent1 = TC.Text;
DropDownList DDL = new DropDownList();
foreach (object s in Enum.GetValues(typeof(Ticket_Status)))
{
DDL.Items.Add(s.ToString());
}
DDL.SelectedValue = CellContent1;
DDL.ID = "I_Status_CB";
e.Row.Cells[2].Controls.Add(DDL);
}
}
Upvotes: 0
Reputation: 34846
Use the ASP.NET PlaceHolder
control in the TemplateField
's ItemTemplate
of your grid view, like this:
<asp:GridView ID="GridView1" runat="server"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Countries">
<ItemTemplate>
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
</asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Now in your code, you can "find" the PlaceHolder1
and add whatever you want to it. I recommend doing this in RowDataBound
event, like this:
protected void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
// Only work with data rows, ignore header or footer rows
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Put logic here to find the placeholder and add controls to it
}
}
Upvotes: 0
Reputation: 3662
Define a container inside your Gridview Cell like asp panel. And add control to the container rather than directly to the Table Cell.
Upvotes: 1