?
Solved

Column width on datagrid

Posted on 2008-10-22
13
Medium Priority
?
509 Views
Last Modified: 2013-12-17
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>

Open in new window

0
Comment
Question by:caoimhincryan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22774146
Hi,

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

<Columns>
            <asp:CommandField ShowSelectButton="True" />
                <asp:BoundField DataField="MyType" HeaderText="MyType" SortExpression="MyType" >
		     <HeaderStyle Width="100px"/>
		 <asp:BoundField>    
               .....
</Columns>

Open in new window

0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22774175
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("tbTextBoxID")
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	
        }
}

Open in new window

0
 
LVL 16

Expert Comment

by:Gyanendra Singh
ID: 22774192
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:caoimhincryan
ID: 22774213
I would be looking to have the textbox outside the Gridview. I will look at your solutions now.
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22774233
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
0
 

Author Comment

by:caoimhincryan
ID: 22774271
I know how many I have in advance. How can I set the size of the textbox based on their ID.
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22774296
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].ItemStyle.Width;

// you might wanna check the casting, it's Unit type so just play around with it

JINN


0
 

Author Comment

by:caoimhincryan
ID: 22774299
My column 1 would have corresponding textbox myTextbox1,
column 2 would have corresponding textbox myTextbox2, etc etc..
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22774323
myTextbox1.Width = mygv.Columns[1].ItemStyle.Width;

JINN
0
 

Author Comment

by:caoimhincryan
ID: 22775955
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?
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22776224
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
0
 

Author Comment

by:caoimhincryan
ID: 22786258
I put it like below and as i step through it, colWidth  = 0 all the time..

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;
            }
 
        }

Open in new window

0
 
LVL 10

Accepted Solution

by:
jinn_hnnl earned 2000 total points
ID: 22794123
That doesn't make sense to me why it returns 0 there.

WHat I can guess is, the GridView finished binding data, but hasn't got it form which leads to the default column width = 0; Have you debugged and check if the data is there in the gridview by the time you get the width? (should be)

1 workaround is moving that part of code to PreRender, which has later execution state in the page life circle

Check the colwidth by that time then.

JINN

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    for(int i = 0; i < LabourCostsGrid.Columns.Count; i++)
            {
                double colWidth = LabourCostsGrid.Columns[i].ItemStyle.Width.Value;
            }
}

Open in new window

0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question