can't fill Gridview w/ manual columns from dataset/datatable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ASPnewb1
    New Member
    • Mar 2008
    • 14

    can't fill Gridview w/ manual columns from dataset/datatable

    I am currently filling a dataTable then adding this table to a dataset, setting the dataset to the Gridview's datasource.

    If I set the Gridview to generate columns automatically it will fill the grid just fine, but I can't get the automated column (column index 1) to be set to readonly. (Column 0 is actually an automated column of edit buttons)

    here's my code:
    Code:
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            ds.Tables.Add(dt);
    
            DataColumn dc = new DataColumn("search #");
            dc.ReadOnly = true; 
           
            dt.Columns.Add(dc);
            dc = new DataColumn("1");
            dt.Columns.Add(dc);
            dc = new DataColumn("2");
            dt.Columns.Add(dc);
            dc = new DataColumn("3");
            dt.Columns.Add(dc);
            dc = new DataColumn("4");
            dt.Columns.Add(dc);
    
            DataRow dr;
    
            object[] myArray = new object[maxElements];
    
            for (int a = 0; a < numSearches; a++)
            {
                for (int b = 0; b < maxElements; b++)
                {
                    myArray[b] = array[a, b];
                }
                dr = dt.NewRow();
                dr.ItemArray = myArray;
                dt.Rows.Add(dr);
            }
    
            GridView2.DataSource = ds;
            GridView2.DataBind();
    notice I do set Column[0] to readonly, but this isn't readonly when binded to the grid, as my edit feature allows for the editing of that column.

    Is this an issue with my edit handler/functionality?

    Now if I set manual columns in the Gridview, this dataset will not fill those columns when binded to the gridview. Will only bind if I have automated columns set.

    ETA: this is an ASP.Net app.

    thanks.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    After you have set the datasource for your GridView and called the DataBind function, go through and look at the columns or your GridView, you should be able to set them to read only there as well, post process.

    Comment

    • ASPnewb1
      New Member
      • Mar 2008
      • 14

      #3
      do I need to do this in the background code or in the ASP/html area?

      I haven't found the path to setting the individual column in the gridview object to read-only.

      meaning I can't find any way to get to readonly from:

      GridView2.Colum ns....

      Comment

      • ASPnewb1
        New Member
        • Mar 2008
        • 14

        #4
        I found a solution but it's somewhat complex (assuming there's still an easier method).


        After DataBind() I do this:


        GridView2.DataB ind();

        for (int x = 0; x < numSearches; x++)
        {
        int x2 = x + 1;

        GridView2.Rows[x].Cells[1].Text = x2.ToString();
        }

        I added another row manually in the GUI editor so this is the Cells[1] that is being edited and numbers of the searches are being put in manually. This column I set to read only.

        Comment

        Working...