I didn't think my idea was too ambitious.
I have a datagrid, and I wanted to populate it entirely from a view in my MSSQL database. That is, I didn't want to specify the column names in the datagrid itself; the datagrid takes the column names straight from what the view specifies. Since I have a templated column (more on that in a minute) that can't be specified in the code-behind code, I can't set the DataGrid to auto-generate columns; that puts my templated column at the beginning of the DataGrid, and I need it at the end. I came up with a nice workaround for this, but it might be causing problems.
Anyway, I also wanted an editable column that allowed the user to choose from a DropDownList when editing. Again, I didn't think it would be a big deal; 4GuysFromRolla has some code I copied, and everything was fine.
Unfortunately, when I added the EditItemColumn and ButtonColumn (for delete), it stopped working. Whenever I try to put any data into my datagrid, I get the following error: "Control 'DataGrid__ctl2__ctl0' of type 'DataGridLinkButton' must be placed inside a form tag with runat=server." I get the error if with either one of the Edit and Button columns in my code. I don't get the error if I take out my templated column.
I really have no idea what's wrong with this code. Can someone help me?
Thanks in advance,
-Starwiz
Here's my HTML code:
And my C# code:
I have a datagrid, and I wanted to populate it entirely from a view in my MSSQL database. That is, I didn't want to specify the column names in the datagrid itself; the datagrid takes the column names straight from what the view specifies. Since I have a templated column (more on that in a minute) that can't be specified in the code-behind code, I can't set the DataGrid to auto-generate columns; that puts my templated column at the beginning of the DataGrid, and I need it at the end. I came up with a nice workaround for this, but it might be causing problems.
Anyway, I also wanted an editable column that allowed the user to choose from a DropDownList when editing. Again, I didn't think it would be a big deal; 4GuysFromRolla has some code I copied, and everything was fine.
Unfortunately, when I added the EditItemColumn and ButtonColumn (for delete), it stopped working. Whenever I try to put any data into my datagrid, I get the following error: "Control 'DataGrid__ctl2__ctl0' of type 'DataGridLinkButton' must be placed inside a form tag with runat=server." I get the error if with either one of the Edit and Button columns in my code. I don't get the error if I take out my templated column.
I really have no idea what's wrong with this code. Can someone help me?
Thanks in advance,
-Starwiz
Here's my HTML code:
Code:
<asp:DataGrid id="DataGrid" (Some datagrid specifications)>
<columns>
<asp:templatecolumn headertext="Status">
<itemtemplate>
<asp:label id="lblStatus" runat="server" text='<%# ((System.Data.DataRowView)Container.DataItem)[StatusField] %>'>
</asp:label>
</itemtemplate>
<edititemtemplate>
<asp:dropdownlist id="cmbStatus" runat="server" datavaluefield="Status"
datatextfield ="Status" DataSource="<%# StatusList %>">
</asp:dropdownlist>
</edititemtemplate>
</asp:templatecolumn>
<asp:editcommandcolumn edittext="Edit" canceltext="Cancel" buttontype="LinkButton"
updatetext="Update" />
<asp:buttoncolumn buttontype="LinkButton" text="Delete" CommandName="Delete"/>
</columns>
</asp:datagrid>
Code:
DataTable tbl;
//Populate tbl
ArrayList columns = new ArrayList(DataGrid.Columns.Count);
while(DataGrid.Columns.Count > 0)
{
DataGridColumn col = DataGrid.Columns[0];
columns.Add(col);
DataGrid.Columns.Remove(col);
}
foreach(DataColumn dc in tbl.Columns)
{
if(dc.ColumnName == "Status")
continue; //status has to be handled separately
BoundColumn bc = new BoundColumn();
bc.DataField = dc.ColumnName;
bc.HeaderText = dc.ColumnName;
bc.ReadOnly = true;
if(dc.ColumnName == "ID")
bc.Visible = false; //Hide the ID column
DataGrid.Columns.Add(bc);
}
foreach(DataGridColumn col in columns)
DataGrid.Columns.Add(col);