I have a problem. I was trying to make a table on a website. Everytime the user add a record it would display on the website. The user can add many more record on the table. My question is how do i make the table display the record automatically without saving the data into a database and retreiving it to dispaly on the the website table?
You can take benefit of gridview, datatable and viewstate.
Initially you can create a datatable and bind it in gridview
Also keep the datatable in the viewstate
Next time when user add some data add a new row in data-table
Update the viewstate data.
And again Bind the gridview.
Edit 1
Adding data to datatable
DataTable dt=new DataTable();
dt.column.Add("Name",typeof(string));
dt.column.Add("Age",typeof(int));
DataRow dr=dt.NewRow();
dr["Name"]="Mohammad"; // or dr[0]="Mohammad";
dr["Age"]=24; // or dr[1]=24;
dt.add.rows(dr);
dr=dt.NewRow();
dr["Name"]="Shahnawaz"; // or dr[0]="Shahnawaz";
dr["Age"]=24; // or dr[1]=24;
dt.add.rows(dr);
GridView1.DataSource=dt;
GridView1.DataBind();
store your datatable in view state
ViewState["dataTable"] = dt;
Now suppose you have text boxes t1 and t2
Datatable dt=ViewState["dataTable"] as DataTable;
dr=dt.NewRow();
dr["Name"]=t1.text; // or dr[0]="Shahnawaz";
dr["Age"]=t2.text; // or dr[1]=24;
dt.add.rows(dr);
Bind it to gridview again
GridView1.DataSource=dt;
GridView1.DataBind();
and keep the datatable in the view state
ViewState["dataTable"] = dt;
Related
I have to show a gridview in my page which doesn't concerns any table to be bound with it. Still it should store one Tax detail per row, as entered by user. Following is the image which will clarify what problem I'm facing.
The gridview shown above should not show any records at first except the footer template of two textboxes and an Add New Record Button, when you are upto add any record.
Once clicked on Add New Record button it should show a row without action column.
(I'm having quite a hard time here)Once clicked on Add/Insert Button(separate from the gridview) store the entered record somehow.
Then I have to show the record in Another concrete GridView with Edit Button.
Once I click on edit button, control should return to my dynamic gridview page. Here, my dynamic gridview will show previously entered record with action column containing a delete button.
Can you Help Me? I don't want help with the code. Just point me in the right direction(s), please.
You should try this.
private DataTable AddEmptyRow()
{
DataTable originalDataTable = GetItems();
DataTable newDataTable = originalDataTable.Clone();
string category = originalDataTable.Rows[0][2].ToString();
foreach (DataRow dRow in originalDataTable.Rows)
{
if (category != dRow[2].ToString())
{
DataRow newDataRow = newDataTable.NewRow();
newDataTable.Rows.Add(newDataRow);
category = dRow[2].ToString();
}
newDataTable.ImportRow(dRow);
}
return newDataTable;
}
You can add row by using DataRow newDataRow = newDataTable.NewRow();.
I want to bind dataset's data to ReportViewer object , I create rdlc report and set it as ReportViewer's report source in design view , but it doesn't show anything ,I checked dataset's data by binding gridview and it's work and show the data in gridview but in ReportViewer object doesn't , Please Help . Here is the code:1(Note: "Temp" is the table's Name of database , that's i want to bind to ReportViewer)2(I don't want to use xsd dataset item)
string path = #"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True";
SqlConnection con1 = new SqlConnection(path);
con1.Open();
const string query = "select * From [Table1] where date between #from and #to";
SqlCommand com1 = new SqlCommand(query, con1);
com1.Parameters.AddWithValue("#from", DateTime.Parse(r1));
com1.Parameters.AddWithValue("#to", DateTime.Parse(r2));
SqlDataAdapter adp1 = new SqlDataAdapter(com1);
DataSet ds1 = new DataSet();
adp1.Fill(ds1);
com1.ExecuteNonQuery();
con1.Close();
GridView1.DataSource = ds1;
GridView1.DataBind();
ReportViewer1.Reset();
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("Temp", ds1.Tables[0]));
ReportViewer1.LocalReport.Refresh();
If you have not done so, you also need to add controls like textboxes or tables to the report and create expressions that reference the fields in the dataset. For example, you could click on the Toolbox in visual studio, and drag a textbox to the report designer. Then right click the textbox, and choose Expression. When the Expression dialog box appears, look in the Category section for Fields(nameofYourDataset), click it, and look in the Values section for the names of fields from your dataset. Double click one of them which will populate the top section, and click OK.
I am updating a GridView dataRow on GridView_RowUpdating event and Updating the database too.
on GridView_RowUpdating event i wrote c# code to update the database and after successfull update of database i am creating a class object of this row. and trying to bind this row to another grid.
Database is getting updated but another grid is not being updated.
Any Solution??
Here is my code
Grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// Update Operation for First Grid
...
...
Class1 ObjRow = new Class1();
ObjRow.Status = Updated.
List<Class1> lst = GetUpdatedDetails();
GridView1.DataSource = lst;
GridView1.DataBind();
lst = new List<Class1>();
lst.add(ObjRow)
// Here Its Failing
GridView2.DataSource = lst;
GridView2.DataBind();
}
Is the second Gridview in an UpdatePanel? Try adding the new row then refreshing the Page. Doe s this have any effect?
I have a datatable in Gridview1 and have formatted it by adding several new rows in the datatable, I want to push this data to another table to also include the blank rows as part of the data. How would I go about doing this? What I mean is I want to use the data in my first gridview as a datasource.
Add your DataTable on into a Session.
Something like:
DataTable dt = new DataTable();
Session["DataStore"] = dt;
Gridview1.DataSource= dt;
GridView1.Databind();
Then you can use Session["DataStore"] as your datasource .
var dt = (DataTable)Session["DataStore"];
GridView2.DataSource = dt;
GridView2.Databind();
Hipe this Helps
Regards
My project has a GridView to display some information. That GridView has some OnRowDataBound processing to massage the data. What I'm trying to do is copy the resulting GridView into a DataTable so I can generate a CSV file. The CSV is generated when the user clicks a button. So the GridView is on the screen and populated and a PostBack occurs. The GridView is not being re-bound on PostBack. What is the recommended way to do this? Here is the code I'm trying currently but I just end up with the headings and blank rows. Debugging confirms that row.Cells[i].Text is empty even though the heading is correct and there is the same number of rows as my GridView contained.
DataTable dataTable = new DataTable("export");
foreach (DataControlField column in myGV.Columns)
{
dataTable.Columns.Add(new DataColumn { ColumnName = column.HeaderText });
}
foreach (GridViewRow row in myGV.Rows)
{
DataRow dataRow = dataTable.NewRow();
for (int i = 0; i < row.Cells.Count; i++)
{
dataRow[myGV.Columns[i].HeaderText] = row.Cells[i].Text;
}
}
You need to work with the data object you're binding to the GridView, not the gridview. There is no hook to the data in the gridview after binding.