Click here to Skip to main content
15,913,927 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: viewstate Pin
Mike Ellison12-Oct-06 6:31
Mike Ellison12-Oct-06 6:31 
Questiontiers Pin
amaneet12-Oct-06 5:31
amaneet12-Oct-06 5:31 
AnswerRe: tiers Pin
Mike Ellison12-Oct-06 6:35
Mike Ellison12-Oct-06 6:35 
Questionproblem with asp.net2.0/sqlserver200 Pin
S.Bhushan12-Oct-06 5:14
S.Bhushan12-Oct-06 5:14 
AnswerRe: problem with asp.net2.0/sqlserver200 Pin
Mike Ellison12-Oct-06 6:37
Mike Ellison12-Oct-06 6:37 
QuestionCreate multilingual web pages very easily Pin
tnsenthil12-Oct-06 5:10
tnsenthil12-Oct-06 5:10 
Questionhow to fix datagrid column width/height when AutoGeneratedColumns is ture? Pin
steven_wong12-Oct-06 5:01
steven_wong12-Oct-06 5:01 
AnswerRe: how to fix datagrid column width/height when AutoGeneratedColumns is ture? Pin
Mike Ellison12-Oct-06 7:02
Mike Ellison12-Oct-06 7:02 
Yea, this can be tricky when auto-generating the columns, because you don't have direct access to the columns. Here's a hack you can try - it involves trapping the PreRender event for the datagrid (this assumes the data is bound prior to PreRender) and manually inspecting the grid's child control Table. Below is an example of what I mean.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>

<script runat="server">
  
    // upon page load, initialize the list
    void Page_Load(object o, EventArgs e)
    {
        if (!IsPostBack)
        {
            myList.DataSource = CreateDataSource();
            myList.DataBind();   
            
            myList2.DataSource = CreateDataSource();
            myList2.DataBind();         
        }
    }
    
  
    // Create the sample datasource for this exercise
    private DataTable CreateDataSource()
    {
        DataTable t = new DataTable();
        
        // create the table structure
        DataColumn c = new DataColumn();
        c.DataType = System.Type.GetType("System.Int32");
        c.ColumnName = "ID";
        c.Unique = true;
        t.Columns.Add(c);
        
        c = new DataColumn();
        c.DataType = System.Type.GetType("System.String");
        c.ColumnName = "Name";
        t.Columns.Add(c);        
        
        c = new DataColumn();
        c.DataType = System.Type.GetType("System.String");
        c.ColumnName = "City";
        t.Columns.Add(c);    
        
        // establish ID as the primary key
        DataColumn[] primaryKey = new DataColumn[1];
        primaryKey[0] = t.Columns["ID"];
        t.PrimaryKey = primaryKey;

        // populate the table with some sample rows of data
        DataRow r = t.NewRow();
        r["id"] = 1001; r["Name"] = "Jack Bauer"; r["City"] = "Los Angeles";
        t.Rows.Add(r);
        
        r = t.NewRow();
        r["id"] = 1002; r["Name"] = "David Palmer"; r["City"] = "Washington D.C.";
        t.Rows.Add(r);
        
        r = t.NewRow();
        r["id"] = 1003; r["Name"] = "Nina Meyers";
        r["City"] = "Los Angeles";  t.Rows.Add(r);

        r = t.NewRow();
        r["id"] = 1004; r["Name"] = "Bill Buchanan";
        r["City"] = "Los Angeles";  t.Rows.Add(r);
        
        return t;
    }
    
    
    private void DG_PreRender(object o, EventArgs e)
    {    
        DataGrid dg = (o as DataGrid);
        if (dg != null)
        {
            Table t = (dg.Controls[0] as Table);
            if (t != null)
            {
                if (t.Rows.Count > 0)
                {
                    foreach(TableCell tc in t.Rows[0].Cells)
                    {
                        tc.Width=new Unit("200px");
                    }
                }
            }
        }
        
    }
    

</script>

<html>
  <head>
    <title>Fixing Columns in a AutoGenerateColumns DataGrid</title>
    <style> body {font-family: 'Tahoma'; font-size: 10pt;}
            td {font-size: 10pt;}
    </style>
  </head>
  
  <body>
    <form runat="server">
        <h3>Fixing Columns in a AutoGenerateColumns DataGrid</h3>

        <p>DataGrid without "fixing"</p>        
        <asp:DataGrid id="myList" runat="server"
                      AutoGenerateColumns="true"
                      />
        
        <br /><br />
        
        <p>DataGrid with "fixing"</p>
        <asp:DataGrid id="myList2" runat="server"
                      AutoGenerateColumns="true"
                      O n P r e R e n d e r="DG_PreRender"
                      />
            <%-- take out the spaces in O n P r e R e n d e r above --%>

    </form>
  </body>
  
</html>

GeneralRe: how to fix datagrid column width/height when AutoGeneratedColumns is ture? Pin
miniThomas12-Oct-06 19:11
miniThomas12-Oct-06 19:11 
GeneralRe: how to fix datagrid column width/height when AutoGeneratedColumns is ture? Pin
Mike Ellison13-Oct-06 6:36
Mike Ellison13-Oct-06 6:36 
GeneralRe: how to fix datagrid column width/height when AutoGeneratedColumns is ture? Pin
steven_wong14-Oct-06 16:24
steven_wong14-Oct-06 16:24 
GeneralRe: how to fix datagrid column width/height when AutoGeneratedColumns is ture? Pin
Mike Ellison15-Oct-06 11:43
Mike Ellison15-Oct-06 11:43 
GeneralRe: how to fix datagrid column width/height when AutoGeneratedColumns is ture? Pin
steven_wong15-Oct-06 14:08
steven_wong15-Oct-06 14:08 
GeneralRe: how to fix datagrid column width/height when AutoGeneratedColumns is ture? Pin
miniThomas15-Oct-06 18:58
miniThomas15-Oct-06 18:58 
GeneralRe: how to fix datagrid column width/height when AutoGeneratedColumns is ture? Pin
Mike Ellison16-Oct-06 8:02
Mike Ellison16-Oct-06 8:02 
GeneralRe: how to fix datagrid column width/height when AutoGeneratedColumns is ture? Pin
Mike Ellison16-Oct-06 8:02
Mike Ellison16-Oct-06 8:02 
QuestionSorting Datagrid Pin
happyheartcs12-Oct-06 2:40
happyheartcs12-Oct-06 2:40 
AnswerRe: Sorting Datagrid Pin
varuni6512-Oct-06 3:04
varuni6512-Oct-06 3:04 
AnswerRe: Sorting Datagrid Pin
postmaster@programmingknowledge.com12-Oct-06 5:13
postmaster@programmingknowledge.com12-Oct-06 5:13 
AnswerRe: Sorting Datagrid Pin
Britney S. Morales12-Oct-06 9:38
Britney S. Morales12-Oct-06 9:38 
Questionhow to delete unuser email userid automatically after 24 hours Pin
dhatchu12-Oct-06 2:34
dhatchu12-Oct-06 2:34 
AnswerRe: how to delete unuser email userid automatically after 24 hours Pin
Vasudevan Deepak Kumar12-Oct-06 4:21
Vasudevan Deepak Kumar12-Oct-06 4:21 
QuestionDisplay OpenFileDialog On click event of ImageButton using ASP.NET & C# Pin
Soniya_k12-Oct-06 2:34
Soniya_k12-Oct-06 2:34 
AnswerRe: Display OpenFileDialog On click event of ImageButton using ASP.NET & C# Pin
Vasudevan Deepak Kumar12-Oct-06 4:22
Vasudevan Deepak Kumar12-Oct-06 4:22 
QuestionProblem : How to Extract The Zip file Pin
Amit Agarrwal12-Oct-06 1:28
Amit Agarrwal12-Oct-06 1:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.