Hello everybody. I find myself in difficulty and ask for help for a situation that is often treated, but separately, namely: DDL dynamically created in GridView and event external to GridView that takes some in value / Text ... x stored in the DB.
They are at the first experiences and the code I report often derives from something else found on the net.
**************************************************
In a WebForm I drew a GridView. Outside the GridView is a Button.
The GridView is made up of 2 BoundFields (of which the first is not visible).
This is the page:
<% @ Page Title = "" Language = "C #" MasterPageFile = "~ / Site.Master" AutoEventWireup = "true" CodeBehind = "WebForm2.aspx.cs" Inherits = "ScheduleWeb.WebForm2"%>
asp: Content>
<% - -%>
asp: BoundField>
asp: BoundField>
asp: GridView>
<% - asp: Panel> -%>
& nbsp; _______________________________________________________________________________________
& nbsp;
asp: Content>
*********************************
In the page Load:
1. I "assume" n°2 DateTime variables that I will need to add the DropDownLists
2. "populate" the mysql oCombo DataTable that I will need for the combos
3. if (! This.IsPostBack) {
... "populate" the DataTable oDataTable_2 by filtering my table by x credentials and date range
... loop this DT x generate the TemplateFields. This is the code:
for (int k = 2; k
{
TemplateField tfield = new TemplateField ();
tfield = new TemplateField ();
tfield.HeaderText = oDataTable_2.Columns [k] .ColumnName;
tfield.ItemStyle.Width = 55;
GridView2.Columns.Add (tfield);
}
... I do the DataBind
}
4. This is the code:
protected void GridView_RowDataBound (Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string NomeGiorno = "";
for (int k = 2; k
{
NomeGiorno = oDataTable_2.Columns [k] .ColumnName.ToString ();
DropDownList ddlCombo = new DropDownList ();
ddlCombo.ID = NomeGiorno;
ddlCombo.DataSource = oCombo;
ddlCombo.DataTextField = oCombo.Columns [0] .ColumnName;
ddlCombo.DataValueField = oCombo.Columns [0] .ColumnName;
ddlCombo.DataBind ();
string country = (e.Row.DataItem as DataRowView) .Row [DayName] .ToString ();
ddlCombo.Width = 55;
ddlCombo.Font.Name = "Arial";
ddlCombo.Font.Size = 8;
ddlCombo.Items.FindByText (country) .Selected = true;
e.Row.Cells [k] .Controls.Add (ddlCombo);
}
}
GridView2.Columns [1] .HeaderStyle.CssClass = "locked";
}
So far everything ok (or at least outwardly). I use DDL, combining items ... everything OK!
But later I want to store the new situation in the DB and so I will have to build
the update query by cycling the columns of the GridView (which has only one row)
But I can't find control !!
I tried this code ...
protected void ButtonOK_Click (object sender, EventArgs e)
{
string cStringa = "update schedule set";
string NomeGiorno = "";
for (int k = 2; k
{// ie after tax code, surname and name.
NomeGiorno = GridView2.Columns [k] .ToString ();
string test = ((DropDownList) GridView2.Rows [0] .FindControl (NomeGiorno)). SelectedItem.Text;
.....
.....
}
}
I get the classic error:
System.NullReferenceException: 'Reference to an object not set on an object instance.'
Thanks to all those x will be able to give me a hand
Alessio
3 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Kiran MohantyPosted Feb 24, 2021, 1:40 PM
alessandro nestaPosted Feb 25, 2021, 10:16 PM
Sachin SinghPosted Feb 24, 2021, 1:45 PM