The Page_Load calls the function, but nothing happens - c#

as the title suggests I have this problem: the function is called, but it does not generate anything for me.
in the function in question I created a DataTable with DataRows, which are subsequently populated. In Page_Load I call the function, but when I launch the debug on the web page nothing appears. The error is probably in the function, because doing a debug I notice that it is called
public partial class ElencoProvince : System.Web.UI.Page
{
public void Page_Load(object sender, EventArgs e)
{
GetTable();
}
public static DataTable GetTable()
{
DataTable table = new DataTable();
table.Columns.Add("Nome", typeof(string));
table.Columns.Add("Cognome", typeof(string));
table.Columns.Add("Azienda", typeof(string));
table.Columns.Add("Provincia", typeof(string));
DataRow row = table.NewRow();
row["Nome"] = "Mario";
row["Cognome"] = "Rossi";
row["Azienda"] = "Pippo";
row["Provincia"] = "Viterbo";
table.Rows.Add(row);
return table;
}
}
Could anyone tell me where I'm wrong?

To see your table you need to add it to a GridView
public void Page_Load(object sender, EventArgs e)
{
GridView gv = new GridView();
gv.ID = "gv" ;
gv.DataSource = GetTable();
gv.DataBind();
this.Controls.Add(gv);
}

Page_Load is a void function and does not return anything.
To view the data you need to bind it to some control on the screen.
You should ideally also check for if it is postback.
Microsoft Docs on Gridview Databind

Related

Data or table in datagridview is not show c#

I want to create a table in datagridview and add a text. I try many of the codes but it does not work.
Here the output even I program to adding some text:
Add columns from cart form:
DataTable table = new DataTable();
private void domainUpDown1_SelectedItemChanged(object sender, EventArgs e)
{
table.Columns.Add("BOOK", typeof(string));
table.Columns.Add("Author", typeof(string));
table.Columns.Add("Price", typeof(double));
table.Columns.Add("Quantity", typeof(int));
dataGridView1.DataSource = table;
}
and add rows from book form:
private void button6_Click(object sender, EventArgs e){
DataTable TBL = new DataTable();
TBL.Rows.Add(BName2.Text);
TBL.Rows.Add(AName2.Text);
TBL.Rows.Add(PRC2.Text);
Ct.dataGridView1.DataSource = TBL;
}
Please someone help me.

Save data records and use it again without querying again into database

I have a two control, which are a Search button and Export button. The user will 'Search' the data and the data will be inserted into DataTable and bound to the GridView or Repeater, and then when user click Export, the DataTable will be passed into the class that will generate an Excel file using the data inside DataTable.
In my current code, the Search button and Export button both will query to fetch data from database. But what I need is only Search button that will query, and store the data into a DataTable or something, and when Export button is clicked it will use the earlier data to generate the Excel file.
Here is my current code:
Report.aspx
<asp:GridView ID="GridView1" runat="server">
<Columns>
</Columns>
</asp:GridView>
</body>
Report.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
/* Page load */
}
protected void btn_search_Click(object sender, EventArgs e)
{
DataTable dt = GetData();
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected DataTable GetData()
{
DataTable dataTable = new DataTable();
/* Query operation */
adapter.Fill(dataTable);
return dataTable;
}
protected void btn_export_Click(object sender, EventArgs e)
{
var excelClass = new ExcelClass();
excelClass.ExcelData = GetData();
}
I attempt to create a DataTable as a property in 'Report.aspx.cs' and fill it with the data when Search is clicked. But when I Export , the DataTable is empty again.
DataTable dataTable = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
/* Page load */
}
protected void btn_search_Click(object sender, EventArgs e)
{
GetData();
GridView1.DataSource = dataTable ;
GridView1.DataBind();
}
protected void GetData()
{
/* Query operation */
adapter.Fill(dataTable);
}
protected void btn_export_Click(object sender, EventArgs e)
{
var excelClass = new ExcelClass();
excelClass.ExcelData = dataTable;
}
But, I need to fetch the data just once, when Search is clicked, and use the data again when I Export it, so it wont query to the database twice.
There are three ways you can achieve this as mentioned below.
Using Session
You can use session for this but this will affect the performance of the application/page if, for example, the dataTable contains 10,000 records.
Add a dataTable into session:
DataTable dataTable= new DataTable();
dataTable= GetData();
Session.Add("dataTable", dataTable);
Retrive that datatable from session:
DataTable dataTable = Session["dataTable"] as DataTable
or
DataTable dataTable= (DataTable)Session["dataTable"];
Export a GridView1.DataSource to a dataTable
DataTable dataTable= Gridview1.DataSource as DataTable;
Iterate each Row from GridView to DataTable
//Create a new DataTable.
DataTable dataTable = new DataTable();
//Add columns to DataTable.
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
dataTable.Columns.Add(cell.Text);
}
//Loop through the GridView and copy rows.
foreach (GridViewRow row in GridView1.Rows)
{
dataTable.Rows.Add();
for (int i = 0; i < row.Cells.Count; i++)
{
dataTable.Rows[row.RowIndex][i] = row.Cells[i].Text;
}
}

Loading multiple textbox value to datagridview

The following code gives me to take one row value from textbox to datagridview ......
private void button1_Click(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("Course Name", typeof(string));
table.Columns.Add("Credit", typeof(string));
table.Rows.Add(txtName.Text.Trim(), txtCredit.Text.Trim());
addcrsView.DataSource = table;
}
When I give another value to textbox it just replaces the previous one. But I need to take both in datagrid view.
As I am very beginner please put your answer details.
You have to make table as class level object.
DataTable table = new DataTable();
And, set the DataGridView datasource in the Form_Load event
private void Form1_Load(object sender, EventArgs e)
{
table.Columns.Add("Course Name", typeof(string));
table.Columns.Add("Credit", typeof(string));
addcrsView.DataSource = table;
}
Your button1_Click just need to add row
private void button1_Click(object sender, EventArgs e)
{
table.Rows.Add(txtName.Text.Trim(), txtCredit.Text.Trim());
}

How to add header columns based on data fetch from the database in gridview

I have a GridView and want to make headers dynamically based on the some SQL query like...
select question from quiz where quizid is 123.
This query will return * number of questions based on the quizid.
How to create headers with the data that's been selected from database?
You can use DataTable to help with this.
I don't know which technologies you used for database management, but I used LinQ to SQL. And the following is my sample:
DataClassesDataContext db = new DataClassesDataContext();
protected DataTable GetDataSource()
{
DataTable dt = new DataTable();
var questions = db.ExecuteQuery<string>("select question from quiz where quizid is 123").ToList();
// Header implementation
int count = 0;
foreach (var question in questions)
{
DataColumn dc = new DataColumn(question);
dt.Columns.Add(dc);
count++;
}
// Rows implementation here
DataRow row = dt.NewRow();
...
dt.Rows.Add(row);
return dt;
}
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = GetDataSource();
GridView1.DataBind();
}
And here is my aspx code:
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
I will suggest you to Add HeaderText Dynamically on RowDataBound event.
You could try something like
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView.Columns[0].HeaderText = "New Header text for First Column";
}
}
You could find RowDataBound on properties >> Events of GridView control. and RowDataBound fires on binding of every row of GridView.

Gridview pagination not working when the gridview is created dynamically by dropdownlist list selection

I have cached a select * query in a dataset in c#.
On my page I have 3 dropdownlists.
Depending on the dropdownlist selected I filter the datatable and bind it to the gridview.
whenever the data in the dropdownlist gets changed,I display the data accordingly.
My problem is, I have added pagination to this gridview since I have more than 500 results for each query.
When I try to paginate,the gridview disappears.The value in the dropdownlist does not change.
what should I do?
the code for page Index changing of gridview is:
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
thank you for you help in advance.
You need to bind your grid again in the function
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
//provide data source here
GridView1.DataBind();
}
Following are the steps that may help you out...
Add dynamic gridview control to page or in any other control where u want to show it on page.
Set its pager settings along with PageIndexChanging Event Handler
Call it in OnInit Event instead of Page_Load
Following is sample code:
private void AddGridDynamically()
{
try
{
HtmlTableRow tr = new HtmlTableRow();
HtmlTableCell td = new HtmlTableCell();
GridView gv = new GridView();
gv.AllowPaging = true;
gv.PageSize = 20;
gv.PagerSettings.Visible = true;
gv.PagerSettings.Mode = PagerButtons.NumericFirstLast;
gv.PageIndexChanging += new GridViewPageEventHandler(gv_PageIndexChanging);
FillGrid(gv);
td.Controls.Add(gv);
tr.Cells.Add(td);
tbl1.Rows.Add(tr);
gv.EmptyDataText="No Data Found";
gv.DataBind();
}
catch (Exception ex)
{
throw ex;
}
}
private void FillGrid(GridView gv)
{
try
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Id");
DataRow dr;
for (int i = 0; i < 100; i++)
{
dr = dt.NewRow();
dr[0] = "AnyThing" + i;
dr[1] = i;
dt.Rows.Add(dr);
}
gv.DataSource = dt;
}
catch (Exception ex)
{
}
}
protected override void OnInit(EventArgs e)
{
AddGridDynamically();
}

Categories

Resources