I am developing a website in which I need to bind a grid several times {inside a for loop}
and as expected the grid overwrites the previous values and preview s the latest iteration result
You may need the code for help.Here it is:-
for (Int32 i = 0; i < k.Length; i++)
{
business.clsplugins obj = new business.clsplugins();
List<business.clspluginsprp> objprp = new List<business.clspluginsprp>();
Int32 z = Convert.ToInt32(k.GetValue(i));
objprp = obj.fnd_plugins(z);
GridView2.DataSource = objprp;
GridView2.DataBind();
}
You need to move your List declaration and GridView assignments to outside the for loop - right now you are creating a new List every iteration, you only want to create one, and then bind that to the Grid.. eg:
List<business.clspluginsprp> objprp = new List<business.clspluginsprp>();
business.clsplugins obj = new business.clsplugins();
for (Int32 i = 0; i < k.Length; i++)
{
Int32 z = Convert.ToInt32(k.GetValue(i));
objprp.Add(obj.fnd_plugins(z));
}
GridView2.DataSource = objprp;
GridView2.DataBind();
Cou cannot bind twice. Then you call DataBind(). The Control rebuild it contents based on the current data source. Old contents are discarded. So you should use a List containg ALL your data, assign it to DataSource and then call DataBind()
Try this one.
This approach may be helpful for you.Get the concept from the code and implement in your way.
ASPX:
<asp:PlaceHolder ID="plcSample" runat="server">
</asp:PlaceHolder>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
DataSet dsSample = GetDataSet();
GridView gvSample;
if (dsSample.Tables.Count > 0 && dsSample.Tables[0].Rows.Count > 0)
{
for (int iCount = 0; iCount < dsSample.Tables.Count; iCount++)
{
gvSample = new GridView();
gvSample.DataSource = dsSample.Tables[iCount];
gvSample.DataBind();
plcSample.Controls.Add(gvSample);
}
}
}
private DataSet GetDataSet()
{
DataSet ds = new DataSet();
DataTable dt;
dt = new DataTable();
dt.Columns.Add(new DataColumn("ID", typeof(string)));
dt.Columns.Add(new DataColumn("Code", typeof(string)));
DataRow dr;
dr = dt.NewRow();
dr["ID"] = 1;
dr["Code"] = "KK";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["ID"] = 2;
dr["Code"] = "Karan";
dt.Rows.Add(dr);
ds.Tables.Add(dt);
dt = new DataTable();
dt.Columns.Add(new DataColumn("ID", typeof(string)));
dt.Columns.Add(new DataColumn("Code", typeof(string)));
dr = dt.NewRow();
dr["ID"] = 1;
dr["Code"] = "AA";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["ID"] = 2;
dr["Code"] = "Arun";
dt.Rows.Add(dr);
ds.Tables.Add(dt);
return ds;
}
Related
I have a dataset where I am pulling value from the database, now after that, I want to add some more rows to it. I get four columns from the database: approverName, approverEmail, approverRole, approvalStatus
DataSet dsResult = new DataSet();
dsResult = getDataFromDatabase();
DataTable dt = new DataTable("MyTable");
dt.Columns.Add(new DataColumn("approverName", typeof(string)));
dt.Columns.Add(new DataColumn("approverEmail", typeof(string)));
dt.Columns.Add(new DataColumn("approverRole", typeof(string)));
dt.Columns.Add(new DataColumn("approvalStatus", typeof(string)));
DataRow dr = dt.NewRow();
dr["approverName"] = "John";
dr["approverEmail"] = "John#gc.com";
dr["approverRole"] = "Manager";
dr["approvalStatus"] = "Pending";
dt.Rows.Add(dr);
dsResult.Tables.Add(dt);
But after executing code I don't see this manually added value in the dataset. It only shows the value which I had after executing line 2 of code
You don't need to create another DataTable.
Append new row to existing DataTable
DataSet dsResult = new DataSet();
dsResult = getDataFromDatabase();
DataTable dt = dsResult.Tables[0]; //Get Reference to existing table
DataRow dr = dt.NewRow();
dr["approverName"] = "John";
dr["approverEmail"] = "John#gc.com";
dr["approverRole"] = "Manager";
dr["approvalStatus"] = "Pending";
dt.Rows.Add(dr);
I am using this code to delete rows from grid view and the rows are getting deleted but when I add a new row to my Grid view the deleted row comes back again any suggestions what should I do to delete row from my grid view and I am not using database on it. Thanks in Advance.
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("EmpId", typeof(string)));
dt.Columns.Add(new DataColumn("EmpName", typeof(string)));
dt.Columns.Add(new DataColumn("DeptName", typeof(string)));
dt.Columns.Add(new DataColumn("EmpAddress", typeof(string)));
dt.Columns.Add(new DataColumn("EmpSalary", typeof(string)));
string Id = string.Empty;
string Name = string.Empty;
string DeptName = string.Empty;
string EmpAddress = string.Empty;
string EmpSalary = string.Empty;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = (GridViewRow)GridView1.Rows[i];
if (i != e.RowIndex)
{
Id = row.Cells[1].Text;
Name = row.Cells[2].Text;
DeptName = row.Cells[3].Text;
EmpAddress = row.Cells[4].Text;
EmpSalary = row.Cells[5].Text;
DataRow dr = dt.NewRow();
dr["EmpId"] = Id;
dr["EmpName"] = Name;
dr["DeptName"] = DeptName;
dr["EmpAddress"] = EmpAddress;
dr["EmpSalary"] = EmpSalary;
dt.Rows.Add(dr);
}
}
GridView1.EditIndex = -1;
GridView1.DataBind();
GridView1.DataSource = dt;
}
My add to row code is this
private void AddNewRecordRowToGrid()
{
// check view state is not null
if (ViewState["EmployeeDetails"] != null)
{
//get datatable from view state
DataTable dtCurrentTable = (DataTable)ViewState["EmployeeDetails"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//add each row into data table
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["EmpId"] = txtEmpId.Text;
drCurrentRow["EmpName"] = txtEmpName.Text;
drCurrentRow["DeptName"] = txtDeptName.Text;
drCurrentRow["EmpAddress"] = txtEmpAddress.Text;
drCurrentRow["EmpSalary"] = txtEmpSalary.Text;
}
//Remove initial blank row
if (dtCurrentTable.Rows[0][0].ToString() == "")
{
dtCurrentTable.Rows[0].Delete();
dtCurrentTable.AcceptChanges();
}
//add created Rows into dataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//Bind Gridview with latest Row
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
}
}
}
Since you load the employees after adding from your ViewState
DataTable dtCurrentTable = (DataTable)ViewState["EmployeeDetails"];
the deleted row wasn't deleted there. I would suggest to edit the DataTable in your ViewState
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
if (ViewState["EmployeeDetails"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["EmployeeDetails"];
dtCurrentTable.Rows.RemoveAt(e.RowIndex);
ViewState["EmployeeDetails"] = dtCurrentTable;
GridView1.EditIndex = -1;
GridView1.DataBind();
GridView1.DataSource = dtCurrentTable;
}
}
and
private void AddNewRecordRowToGrid()
{
if (ViewState["EmployeeDetails"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["EmployeeDetails"];
DataRow drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["EmpId"] = txtEmpId.Text;
drCurrentRow["EmpName"] = txtEmpName.Text;
drCurrentRow["DeptName"] = txtDeptName.Text;
drCurrentRow["EmpAddress"] = txtEmpAddress.Text;
drCurrentRow["EmpSalary"] = txtEmpSalary.Text;
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["EmployeeDetails"] = dtCurrentTable;
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
}
}
Why does SqlDataAdapter's Fill method not allow me to add a row with the same its own rows' value as appearance? I could not success to provide a row's value that appears at the same row with filled one in DataTable.
Check this out:
using (SqlDataAdapter a = new SqlDataAdapter("SELECT SIPNO, SERINO, TARIH FROM SNOHAREKETLER WHERE Cast(TARIH as DATE) BETWEEN '2015/03/19' AND '2015/03/20' AND (TEZNO = 'T23' OR TEZNO = 'T31') AND CIKTI is null", c))
{
// 3
// Use DataAdapter to fill DataTable
DataTable t = new DataTable();
a.Fill(t);
t.Columns.Add("MyColumn", typeof(string));
DataRow workRow;
int iGetCount = t.Rows.Count;
for (int i = 0; i <= iGetCount - 1; i++)
{
workRow = t.NewRow();
workRow["MyColumn"] = i;
t.Rows.Add(workRow);
}
// 4
// Render data onto the screen
dataGridView1.DataSource = t;
}
Here is the solution of my issue. I searched and consulted someone. My issue was trying to add a NewRow and this causes the issue.
DataTable t = new DataTable();
a.Fill(t);
DataColumn newCol = new DataColumn("NewColumn", typeof(string));
newCol.AllowDBNull = true;
t.Columns.Add(newCol);
foreach (DataRow row in t.Rows)
{
row["NewColumn"] = "With String";
}
dataGridView1.DataSource = t;
public void BindGrid(int rowcount)
{
btnsubmit.Visible = true;
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("Employee ID", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("Leave Type", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("From", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("To", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("No:of Days", typeof(String)));
if (ViewState["CurrentData"] != null)
{
for (int i = 0; i < rowcount + 1; i++)
{
dt = (DataTable)ViewState["CurrentData"];
if (dt.Rows.Count > 0)
{
dr = dt.NewRow();
dr[0] = dt.Rows[0][0].ToString();
}
}
dr = dt.NewRow();
dr[0] = lblempID.Text;
dr[1] = ddlleavetype.SelectedItem.Text.ToString();
dr[2] = txtfrom.Text.ToString();
dr[3] = txtto.Text.ToString();
dt.Rows.Add(dr);
}
else
{
dr = dt.NewRow();
dr[0] = lblempID.Text;
dr[1] = ddlleavetype.SelectedItem.Text.ToString();
dr[2] = txtfrom.Text.ToString();
dr[3] = txtto.Text.ToString();
dt.Rows.Add(dr);
}
// If ViewState has a data then use the value as the DataSource
if (ViewState["CurrentData"] != null)
{
gvLeave.DataSource = (DataTable)ViewState["CurrentData"];
gvLeave.DataBind();
}
else
{
// Bind GridView with the initial data assocaited in the DataTable
gvLeave.DataSource = dt;
gvLeave.DataBind();
}
// Store the DataTable in ViewState to retain the values
ViewState["CurrentData"] = dt;
}
In my page having from date textbox and todate textbox..i want to calculate the between two dates ,please help me
I'm not very familiar with the DataTable object, so take this answer as an idea. :-)
Why don't you specify the Rows with the actual type?
dt.Columns.Add(new System.Data.DataColumn("From", typeof(DateTime)));
dt.Columns.Add(new System.Data.DataColumn("To", typeof(DateTime)));
dt.Columns.Add(new System.Data.DataColumn("No:of Days", typeof(int)));
And after this you can Parse the data of txtfrom like dr[2] = DateTime.Parse(txtfrom.Text.ToString()) and assign it to your DataRow. Same goes for txtto.
After this, you are able to calculate the days between by using:
dr[4] = (int)((dr[2] -dr[3]).TotalDays);
If this doesn't work, try to save the parsed Dates in objects like so:
DateTime from = DateTime.Parse(txtfrom.Text.ToString());
DateTime to = ...;
TimeSpan t1 = to - from;
dt[4] = (int)(t1.TotalDays);
If they are in a textbox you will need to parse them first using DateTime.Parse. This is under the assumption that the strings are in the correct format
DateTime myDate = DateTime.Parse(myDateString);
To calculate the days between two dates you can try this
TimeSpan span =date1-date2;
var days = span.TotalDays;
Try this Code :
Var Day= DateTime.Parse(todate.Text).Subtract(DateTime.Parse(fromdate.Text)).Days;
Im new in asp.net. I want to know how to add a row in a gridview programatically. I was able to do it but it just displays the latest addition.
Here is my code:
DataTable dt = new DataTable();
dt.Columns.Add("Question");
dt.Columns.Add("Answer");
DataRow dr = dt.NewRow();
dr["Question"] = txtQuestion.Text;
dr["Answer"] = txtAnswer.Text;
dt.Rows.Add(dr);
dt.AcceptChanges();
gvQnA.DataSource = dt;
gvQnA.DataBind();
Its because you are creating new table each time and binding it with the grid
Do code as below may resolve your issue ...
here i am taking existing datasource and binding it again by adding two more row...
DataTable dt = gridView.DataSource as DataTable;
if (dt != null)
{
DataRow dr = dt.NewRow();
dr["Question"] = txtQuestion.Text;
dr["Answer"] = txtAnswer.Text;
dt.Rows.Add(dr);
dt.AcceptChanges();
gvQnA.DataSource = dt;
gvQnA.DataBind();
}
#Pranay is correct.In addition You can also achive that by using DataTable as property.
private DataTable Dt
{
set { ViewState.Add("Dt", value); }
get { return (DataTable)ViewState["Dt"]; }
}
...
DataRow dr = Dt.NewRow();
dr["Question"] = txtQuestion.Text;
dr["Answer"] = txtAnswer.Text;
Dt.Rows.Add(dr);
Dt.AcceptChanges();
gvQnA.DataSource = Dt;
gvQnA.DataBind();
You have added one row in your code thats why it is showing one row.
If you have added multiple rows it would have shown proper result
DataTable dt = new DataTable();
dt.Columns.Add("Question");
dt.Columns.Add("Answer");
DataRow dr = dt.NewRow();
dr["Question"] = txtQuestion.Text;
dr["Answer"] = txtAnswer.Text;
dt.Rows.Add(dr);
**DataRow dr = dt.NewRow();
dr["Question"] = "2nd row";
dr["Answer"] = "2nd row";
dt.Rows.Add(dr);**
dt.AcceptChanges();
gvQnA.DataSource = dt;
gvQnA.DataBind();
May be #Pranay is also right
Hey Just check this. This might help u
DataTable dataTable = new DataTable();
int columnsCount = // Set the number of the table's columns here.
for (int columnIndex = 0; columnIndex < columnsCount; columnIndex++)
{
DataColumn dataColumn = new DataColumn();
// Assign dataColumn properties' values here ..
dataTable.Columns.Add(dataColumn);
}
int rowsCount = // Set the number of the table's rows here.
for (int columnIndex = 0; columnIndex < columnsCount; columnIndex++)
{
DataRow dataRow = new DataRow();
dataRow["ColumnName"] = // Set the value here ..
dataTable.Rows.Add(dataRow);
}