How can the width of an edit text box be set in a GridView control where EnableSortingAndPagingCallback is true? Microsoft’s documentation for GridView.EnableSortingandPagingCallbacks states "If the Columns collection contains a column that does not support callbacks, such as TemplateField, a NotSupportedException exception is raised." So this rules out the use of a template column. I tried the template column and found it caused an error as advertised. I posted this question on Express Exchange but received only one bogus response.
Here is my GridView code…
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%"
AllowSorting="True" AllowPaging="true" PageSize="12"
PagerSettings-Mode="Numeric"
OnRowDataBound="BlackberryView_OnRowDataBound"
EnableSortingAndPagingCallbacks="True" EmptyDataText="No records found!"
DataSourceID="SqlDataSource1" AutoGenerateEditButton="True"
OnRowUpdating="BlackberryView_OnRowUpdating" EditRowStyle-Width="100%">
<HeaderStyle CssClass="blackberryviewheader" ForeColor="White" Wrap="False" />
<RowStyle CssClass="blackberryviewrow" ForeColor="#003366" />
<AlternatingRowStyle CssClass="blackberryviewalternatinfrow" />
<EditRowStyle BackColor="Red" Width="100%" />
<PagerStyle ForeColor="#003366" Font-Bold="true" HorizontalAlign="Center" />
</asp:GridView>
After much searching of the internet, and only one bogus response from "Experts Exchange" I set to work my to solve the problem myself. Here is my work around…
- Create a integer variable, editRow, and set its value to -2
- When the OnEditRowEditing event fires, set editRow to the value of the selected row index.
- As the ViewGrid re-builds, check for the row selected for editing. When found, check the specific cell for an empty string. If the cell contains a text box control the cell Text value will be "".
- Next cast the cell’s first control to a TextBox then you can set its various properties including it’s width.
Int32 editRow = -2;
protected void GridView1_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
TextBox tb = null;
if (e.Row.RowIndex == editRow)
{
sVal = e.Row.Cells[2].Text;
if (e.Row.Cells[2].Text == "")
{
tb = (TextBox)e.Row.Cells[2].Controls[0];
tb.Wrap = true;
tb.Width = Unit.Pixel(740);
tb.Rows = 4;
tb.TextMode = TextBoxMode.MultiLine;
tb.Height = Unit.Pixel(60);
}
}
}
protected void GridView1_OnRowEditing(object sender, GridViewEditEventArgs e)
{
editRow = e.NewEditIndex;
}
Hi, Mike! I’m as forgetful as ever and can’t remember where I put your email address. I would love to chat with you about notebooks and your opinions, if you’ll indulge me. Thanks! Sylvia
LikeLike
Thanks a lot for the post that help me setting the width of the Datagrid edit cell width.
But i cant understand why is the editRow set to -2 at first?
editRow = -2
Thanks
LikeLike