caoimhincryan
asked on
Column width on datagrid
I have a datagrid below. What I need is to have text box's directly above every column. How can I keep them the same width as the grid view column?
<asp:GridView ID="MyGrid" runat="server" DataKeyNames="MyType" AutoGenerateColumns="False" DataSourceID="MyDataSource" AllowPaging="True">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="MyType" HeaderText="MyType" SortExpression="MyType" />
<asp:BoundField DataField="Category1" HeaderText="Category1"
SortExpression="Category1" />
<asp:BoundField DataField="Category2" HeaderText="Category2"
SortExpression="Category2" />
</Columns>
</asp:GridView>
Well, I think I misunderstood your question, you wanted to set the width of the TextBox appropriately to the Width of the column??
You will have to do it in RowDataBound event of the gridView, is that you want the textBOx inside your grid or outside? you can get in there, count column
e.Row.Cells.Count
and on each colum you get the width of that column:
int colWidth = e.Row.Cells[index].Width
Then you find your textBox control by:
TextBox myTB = e.Row.Cells[3].FindControl ("tbTextBo xID")
myTB.width = colWidth
Hope it gives you some idea
JINN
You will have to do it in RowDataBound event of the gridView, is that you want the textBOx inside your grid or outside? you can get in there, count column
e.Row.Cells.Count
and on each colum you get the width of that column:
int colWidth = e.Row.Cells[index].Width
Then you find your textBox control by:
TextBox myTB = e.Row.Cells[3].FindControl
myTB.width = colWidth
Hope it gives you some idea
JINN
protected void gvShaders_RowDataBound(object sender, GridViewRowEventArgs e)
{
//.... depend on how you want to put your textbox more condition will be added and changed
if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
{
/...another tracking to find row of the textBox
//and on each colum you get the width of that column:
int colWidth = e.Row.Cells[index].Width
//Then you find your textBox control by:
TextBox myTB = e.Row.Cells[3].FindControl("tbTextBoxID")
myTB.width = colWidth
}
}
ASKER
I would be looking to have the textbox outside the Gridview. I will look at your solutions now.
ok then you dont have to user RowDataBound, do you know in advance how many textbox you have or you create it after the GV is binded
If so, you need a placeHolder: just play around to get the layout right the way you want it
for(int i = 0; i < myGV.Columns.Count; i++)
{
int colWidth = myGV.Columns[i].ItemStyle. Width
TextBox myTextBox = new TextBox();
myTextBox.Width = colWidth;
plhBoxes.Add(myTextBox);
}
JINN
If so, you need a placeHolder: just play around to get the layout right the way you want it
for(int i = 0; i < myGV.Columns.Count; i++)
{
int colWidth = myGV.Columns[i].ItemStyle.
TextBox myTextBox = new TextBox();
myTextBox.Width = colWidth;
plhBoxes.Add(myTextBox);
}
JINN
ASKER
I know how many I have in advance. How can I set the size of the textbox based on their ID.
ok, as I posted.
for example if you have this textBox
<asp:TextBox ID="tbSearchName" runat="server"/>
And the Colum: "CusName" is at index 2;
then after the gridView binding in your .cs file:
tbSearchName.Width = "200px";
or bSearchName.Width = gvShaders.Columns[1].ItemS tyle.Width ;
// you might wanna check the casting, it's Unit type so just play around with it
JINN
for example if you have this textBox
<asp:TextBox ID="tbSearchName" runat="server"/>
And the Colum: "CusName" is at index 2;
then after the gridView binding in your .cs file:
tbSearchName.Width = "200px";
or bSearchName.Width = gvShaders.Columns[1].ItemS
// you might wanna check the casting, it's Unit type so just play around with it
JINN
ASKER
My column 1 would have corresponding textbox myTextbox1,
column 2 would have corresponding textbox myTextbox2, etc etc..
column 2 would have corresponding textbox myTextbox2, etc etc..
myTextbox1.Width = mygv.Columns[1].ItemStyle. Width;
JINN
JINN
ASKER
That doesnt seem to work for me now.
Im actually thinking of now trying to have the textboxes inside my grid. Is that what you have above?
Im actually thinking of now trying to have the textboxes inside my grid. Is that what you have above?
WHere do you put this?
myTextbox1.Width = mygv.Columns[1].ItemStyle. Width;
You have to make sure you set this after the GirdView has already been binded. If you want to put the boxes inside the gridView, it's different story, quite tricky I assume (because you only want 1 box for each column)
JINN
myTextbox1.Width = mygv.Columns[1].ItemStyle.
You have to make sure you set this after the GirdView has already been binded. If you want to put the boxes inside the gridView, it's different story, quite tricky I assume (because you only want 1 box for each column)
JINN
ASKER
I put it like below and as i step through it, colWidth = 0 all the time..
Any ideas?
Any ideas?
protected void LabourCostsGrid_DataBound(object sender, EventArgs e)
{
for(int i = 0; i < LabourCostsGrid.Columns.Count; i++)
{
double colWidth = LabourCostsGrid.Columns[i].ItemStyle.Width.Value;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can give them the style you want, manipulate it as much as you can by setting the header style
Do it for the column that you think it might be applied.
JINN
Open in new window