databind a gridview populated in the code behind
I have tried a lot and have reached a dead end.
I have a to show multiple gridviews on one page and all these are getting populated dynamically.
i have figured a way to populate the gridview dynamically and display them, but i cannot get how to modify these values and display them as i want.
here is the code. for .aspx page
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
<asp:Panel ID="Panel1" runat="server" ScrollBars ="Auto" Height="500px" BorderColor="#003399" BorderWidth="3px">
</asp:Panel>
</asp:Content>
and here is the code for .aspx.cs
for (int j = 0; j < 5; j++)
{
GridView Grid = new GridView();
ArrayList machID = new ArrayList();
ArrayList machName = new ArrayList();
Grid.ID = machGrps[j].ToString();
Grid.AllowSorting = true;
Grid.CellSpacing = 2;
Grid.GridLines = GridLines.None;
Grid.Width = Unit.Percentage(100);
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection(Session["ConnectionStringSQL"].ToString());
connection.Open();
SqlCommand sqlCmd = new SqlCommand("select MachineID, MachineName from Machines", connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlDa.Fill(dt);
connection.Close();
if (dt.Rows.Count > 0)
{
for (int i = 0; i < d开发者_如何学JAVAt.Rows.Count; i++)
{
machID.Add(dt.Rows[i]["MachineID"].ToString());
machName.Add(dt.Rows[i]["MachineName"].ToString());
}
DataTable taskTable = new DataTable();
taskTable.Columns.Add("MachineID");
taskTable.Columns.Add("MachineName");
if (machID.Count != 0)
{
for (int i = 0; i < machID.Count; i++)
{
DataRow tableRow = taskTable.NewRow();
tableRow["MachineID"] = machID[i];
tableRow["MachineName"] = machName[i];
taskTable.Rows.Add(tableRow);
}
}
Session["TaskTable"] = taskTable;
Grid.DataSource = Session["TaskTable"];
Grid.DataBind();
Label label = new Label();
label.Text = "Machine Grp" + Grid.ID;
Panel1.Controls.Add(label);
Panel1.Controls.Add(Grid);
}
}
NOTE THIS CODE BELOW CANNOT BE DONE ANYMORE AS I POPULATE THE GRIDVIEW DIRECTLY FROM THE TABLES:
I used to modify like this when the ondatabound="GridView1_DataBound" if is called from the .aspx page
the databound code looks like this:
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow myRow in GridView1.Rows)
{
//this is where ID is stored in an label template
Label Label3 = (Label)myRow.FindControl("Label3");
int val = Convert.ToInt32(Label3.Text);
Image Image5 = (Image)myRow.FindControl("Image5");
if (Convert.ToInt32(Label3.Text) < 0)
{
Image5.ImageUrl = "~/NewFolder1/warning_16x16.gif";
}
else
{
Image5.ImageUrl = "~/NewFolder1/1258341827_tick.png";
}
}
}
Two things:
protected void GridView1_DataBound(object sender, EventArgs e) {
foreach (GridViewRow myRow in GridView1.Rows) {
// blah blah blah code goes here
}
}
should be
//now e is a
protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
// blah blah blah, this lets you work on one row at a time.
// since the framework does this per row, you don't need to parse the gridview
// yourself, altho you're more than welcome to.
// But you can get more control this way. I promise.
Label Label3 = (Label)e.Row.FindControl("Label3");
int val = Convert.ToInt32(Label3.Text);
Image Image5 = (Image)e.Row.FindControl("Image5");
if (val < 0)
{
Image5.ImageUrl = "~/NewFolder1/warning_16x16.gif";
}
else
{
Image5.ImageUrl = "~/NewFolder1/1258341827_tick.png";
}
// alternately write the code above like this:
Image Image5 = (Image)e.Row.FindControl("Image5");
Image5.ImageUrl = (val < 0) ? "~/NewFolder1/warning_16x16.gif" : "~/NewFolder1/1258341827_tick.png";
// that's called the ternary operator. Takes up less space, does the same thing.
}
}
Please don't ignore this part
Now to the other part, are you aware that you can still bind in the backside like thus:
GridView1.RowDataBound += GridView1_RowDataBound;
Put it here:
for (int j = 0; j < 5; j++)
{
GridView Grid = new GridView();
ArrayList machID = new ArrayList();
ArrayList machName = new ArrayList();
//NEW LINE (doesn't need the comment or the blank space tho)
Grid.RowDataBound += Grid_RowDataBound;
Grid.ID = machGrps[j].ToString();
Grid.AllowSorting = true;
Grid.CellSpacing = 2;
Grid.GridLines = GridLines.None;
Grid.Width = Unit.Percentage(100);
Do your dynamic edits on GridView1_RowDataBound not on GridView1_DataBound
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label Label3 = (Label)e.Row.FindControl("Label3");
....
}
}
Is it blowing up when you're binding the DataSource to the Session object?
Grid.DataSource = taskTable;
精彩评论