GridView in C#.net 3.5

E

Ethan Strauss

Hi,
I want to use a Gridview control to display a list of item names each
associated with two checkboxes. The goal is for the user to be able to
designate two Boolean characteristics of each item. The list of names will
NOT be coming from a database. This seems simple, but I can't figure out how
to do it.
I have tried creating three columns on the .ascx page. Two checkbox columns
and one bound column for the items names. I can't get any of these columns to
populate from code. When I set the datasource to a List<string> of the
names, I get nothing I AutoGenerateColumns == false and a new column added
with the names if AutoGenerateColumns == true. I have creating all three
columns from code and I can get the columns to exist, but I never see any
checkboxes.
Does anyone know of a good example which does not use binding to a SQL type
datasource?
Thanks!
Ethan
Here is the code I have used to add all the columns programmatically. I see
four columns, but only the autogenerated one has any text or visible controls
in it.
DataControlField UseSequence = new CheckBoxField();
UseSequence.Visible = true;
DataControlField ReverseComplement = new CheckBoxField();
DataControlField SequenceName = new BoundField();
GridView1.Columns.Add(UseSequence);
GridView1.Columns.Add(ReverseComplement);
GridView1.Columns.Add(SequenceName);
int index = 0;
foreach (string name in sequenceNames)
{
DataControlFieldCell cell = new
DataControlFieldCell(SequenceName);
cell.Text = name;
SequenceName.InitializeCell(cell,
DataControlCellType.DataCell, DataControlRowState.Normal, index);

DataControlFieldCell useCell = new
DataControlFieldCell(UseSequence);
useCell.Controls.Add(new CheckBox());
UseSequence.InitializeCell(useCell,
DataControlCellType.DataCell, DataControlRowState.Normal, index);
DataControlFieldCell rcCell = new
DataControlFieldCell(ReverseComplement);
ReverseComplement.InitializeCell(rcCell,
DataControlCellType.DataCell, DataControlRowState.Normal, index);
index++;
}
GridView1.AutoGenerateColumns = true;
GridView1.DataSource = sequenceNames;
GridView1.DataBind();

GridView1.Visible = true;
GridView1.BackColor = System.Drawing.Color.Pink;
 
A

Andy O'Neill

Ethan Strauss said:
Hi,
I want to use a Gridview control to display a list of item names each
associated with two checkboxes. The goal is for the user to be able to
designate two Boolean characteristics of each item. The list of names will

This is air code, but give you the idea.
Something like
In your code behind, define a type

private class whatever
{
public string field1 {get; set;}
public int checked1 {get, set;} // or maybe bool but you get the idea
etc
}

in page_load or whatever ( remember postback )

// typed list based of the whatever type
List<whatever> lw = new List<whatever>();
lw.Add (new whatever("aaa", 0));
lw.Add (new whatever("bbb", 0));
GridView1.Datasource =lw;
GridView1.Databind;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top