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());
}
Related
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
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.
How can I display the first selected row values to text boxes after I filter the data in the datagridview?
private void btnsearch_Click(object sender, EventArgs e)
{
dgpay.DataSource = p.SearchInPaymentVouchers("PaymentVouchers.VendorID", comven.SelectedValue.ToString());
}
private void btnsearch_Click(object sender, EventArgs e)
{
DataTable dt = p.SearchInPaymentVouchers("PaymentVouchers.VendorID", comven.SelectedValue.ToString());
dgpay.DataSource = dt;
//Code to bind first row in textbox.
//check if the datatable has rows
if(dt.Rows.Count > 0)
{
textbox.text = Convert.ToInt32(dt.Rows[0]["id"]);
//Row[line index you want to get]["Header of datatable column ex VendorID"]
}
}
Hope this is what you actually want. Please feel free to ask if you face any problem.
When button_click event is fired. I get DataTable from method and assign it to DataTable defined in class, but this DataTable setting in nothing. If I bind DataTable to GridView it show data.
public partial class Default : System.Web.UI.Page
{
TestClass test = new TestClass();
DataTable _table = new DataTable();
protected void Button1_Click(object sender, EventArgs e)
{
_table = test.getTable();
//displaying table
GridView1.DataSource= _table;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
//does not displaying table
GridView2.DataSource= _table;
GridView2.DataBind();
}
}
When I want get some data from _table, it have nothing. What's wrong?
Update:
Thanks all, and especially Darren Davies, problem been in Postback. When Button2_Click event happens _table assign _table = new DataTable(), and therefore _table no more reference to table returned by method getTable().
It will only bind correctly if Button1 is clicked before Button2. Otherwise _table will not be populated.
You should check that _table has data or call the getTable method inside the Button2_Click event handler:
protected void Button2_Click(object sender, EventArgs e)
{
_table = test.getTable();
GridView2.DataSource= _table;
GridView2.DataBind();
}
What you could try is this:
protected void Button1_Click(object sender, EventArgs e)
{
_table = test.getTable();
//displaying table
GridVieuw1.Datasource = null;
GridView1.DataSource= _table;
GridView1.DataBind();
}
Also you must do this at buuton 2
protected void Button2_Click(object sender, EventArgs e)
{
_table = test.getTable();
//displaying table
GridVieuw2.Datasource = null;
GridView2.DataSource= _table;
GridView2.DataBind();
}
Otheriwse the table is empty at button 2, so you have to press button 1 to fill it
Maybe that can do the trick
You are initializing your DataTable like
DataTable _table = new DataTable();
Then, when you click your button 1:
protected void Button1_Click(object sender, EventArgs e)
{
_table = test.getTable(); // <- here
//displaying table
GridView1.DataSource= _table;
GridView1.DataBind();
}
you are assigning your DataTable to a different instance.
You have to debug and check that your method getTable is returning a valid DataTable (and if you want to see your data, make sure that it contains data.
Your problem arises when you do not press button 1 before button 2. You should generalized your data binding like this:
public partial class Default : System.Web.UI.Page
{
TestClass test = new TestClass();
DataTable _table = new DataTable();
protected void Button1_Click(object sender, EventArgs e)
{
FillDataTable (); // <----------- See this function call
//displaying table
GridView1.DataSource= _table;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
FillDataTable (); // <----------- See this function call
//does not displaying table
GridView2.DataSource= _table;
GridView2.DataBind();
}
private void FillDataTable()
{
if(_table.Rows.Count == 0)
_table = test.getTable();
}
}
It would a wise way to fill table once, instead of filling on every button click and write it down in every button click event.
I have populated a list box using a Data-table.
In another method, I have to retrieve the data table from the list box.
Datatable dt = (DataTable)lstExample.DataSource;
Throws an error, that the Datatable dt is null
I am working on c# ASP.Net
If you are trying to do this on a Postback then the DataTable will no longer be there. You will need to save it in ViewState or Session if you want access to it on a Postback, or just hit the database again.
For example:
protected override Page_Load(object sender, EventArgs e)
{
if( !IsPostBack)
{
DataTable tbl = GetData();
lstData.DataSource = tbl;
lstData.DataBind();
// store in viewstate
ViewState["data"] = tbl;
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
DataTable tbl = (DataTable)ViewState["data"];
}