I have two gridviews in my asp.net page. Where the datas are coming from two different databases. In that, one gridview has select row function. Now what I want to do is, I need to generate my 3rd gridview row by row according to the selection of the 1st gridview's 2 cell and the 2nd gridview 1st column data(1st column data is common for all the rows).
This is my code:
DataTable dt = new DataTable();
var EmpID = (Label)grdEmp.SelectedRow.Cells[1].FindControl("lblEmpID");
var firstName = (Label)grdEmp.SelectedRow.Cells[1].FindControl("lblFirstName");
var date = DateTime.Now.ToString();
var planID = (Label)grdPlanID.Rows[0].Cells[1].FindControl("lblPlanID");
string empid = Convert.ToString(EmpID.Text);
string first = Convert.ToString(firstName.Text);
string Date = Convert.ToString(date);
string planid = Convert.ToString(planID.Text);
dt.Columns.Add("Emp_ID", typeof(string));
dt.Columns.Add("First_Name", typeof(string));
dt.Columns.Add("Created_Time", typeof(string));
dt.Columns.Add("Plan_ID", typeof(string));
DataRow dtrow = dt.NewRow(); // Create New Row
dtrow["Emp_ID"] = empid; //Bind Data to Columns
dtrow["First_Name"] = first;
dtrow["Created_Time"] = Date;
dtrow["Plan_ID"] = planid;
dt.Rows.Add(dtrow);
grdList.DataSource = dt;
grdList.DataBind();
Related
How can i filter the rows using textBox when rows are added manually
Here is my code:
int n = XML_Grid.Rows.Add();
XML_Grid.ClearSelection();
XML_Grid.Rows[n].Cells[1].Value = FileName;
XML_Grid.Rows[n].Cells[2].Value = Name;
XML_Grid.Rows[n].Cells[3].Value = ID;
XML_Grid.Rows[n].Cells[4].Value = Date;
If not possible to filter how can i add the rows using datatable like i did for the DataGridView in the same way?
Note: This question could be duplicate but i didn't find any solution to my problem
Load your Xml data to DataTable and then set this DataTable to XML_Grid.DataSource.
DataTable dt = new DataTable();
dt.Clear();
dt.Columns.Add("FileName");
dt.Columns.Add("Name");
dt.Columns.Add("ID");
dt.Columns.Add("Date");
XML_Grid.Rows.Clear();
lbl_Path.Text = fbd.SelectedPath;
string[] files = Directory.GetFiles(fbd.SelectedPath, "*.xml");
XmlDocument doc = new XmlDocument();
XmlNodeList nodes = doc.GetElementsByTagName("cfdi:Emisor");
XmlNodeList nodes1 = doc.GetElementsByTagName("cfdi:Comprobante");
foreach (string tot_file in files)
{
doc.Load(tot_file);
string FileName = Path.GetFileNameWithoutExtension(tot_file);
for (int i = 0; i < nodes.Count; i++)
{
string Name = nodes[i].Attributes["Nombre"].Value;
string ID = nodes[i].Attributes["Rfc"].Value;
string Date = nodes1[i].Attributes["Fecha"].Value;
DataRow row = dt.NewRow();
row["FileName"] = FileName;
row["Name"] = Name;
row["ID"] = ID;
row["Date"] = Date;
dt.Rows.Add(row);
}
}
XML_Grid.DataSource = dt;
And in your textbox textchanged event add below like to filter data.
(XML_Grid.DataSource as DataTable).DefaultView.RowFilter = string.Format("FileName LIKE '{0}%'", txt_FileName.Text)
Edit:
If you want to add the rows to your existing columns that you added through add column from datagridview then just set DataPropertyName for each of column of XML_Grid to column name of DataTable like
XML_Grid.Columns[0].DataPropertyName = "FileName";
XML_Grid.Columns[1].DataPropertyName = "Name";
XML_Grid.Columns[2].DataPropertyName = "ID";
XML_Grid.Columns[3].DataPropertyName = "Date";
Where 0,1,2,3 are the index of your column. Verify your column index respective to you existing columns in XML_Grid.
Add above line of code just above to XML_Grid.Rows.Clear();
Alternative to above code is you can set DataPropertyName for each of column from Property Window of datagridview.
Select XML_Grid ==> Open Property Window ==> Select Columns Property ==> Choose your column ==> And set its DataPropertyName from (none) to "FileName".
And same for all of your remaining columns.
I have this part of code and when I add a row into a DataView the first row into the DataTable changes with the last row added after the first one.
DataRow drSeguro = this._dvSegurosVinculados.Table.NewRow();
drSeguro["RamoSeguro"] = Ramo;
drSeguro["Operacion"] = operacion;
drSeguro["Aseguradora"] = aseguradora;
aux = Convert.ToDecimal(this.bfSumaAsegurada.DataEntryText);
drSeguro["SumaAsegurada"] = suma;
drSeguro["Propuesta"] = this.psHeader.IdSolicitud;
this.BindingContext[this._dvSegurosVinculados].SuspendBinding();
this._dvSegurosVinculados.Table.Rows.Add(drSeguro);
this.BindingContext[this._dvSegurosVinculados].ResumeBinding();
I am trying to bind DataTable to DataGridView as below;
DataTable table = new DataTable();
foreach (ConsumerProduct c in ctx.ConsumerProducts)
{
DataRow row = table.NewRow();
row["Id"] = c.ID;
row["Model"] = c.Model;
row["Status"] = "Offline";
table.Rows.Add(row);
}
dataGridView1.DataSource = table;
I get exception at the first line itself row["Id"]
Column 'Id' does not belong to table .
P.S. I have already added these columns in the designer view of dataGridView.
You just created a blank DataTable and then you are trying to add data to particular columns like Id, Model and Status.
You have to add those columns as well.
DataTable table = new DataTable();
table.Columns.Add("Id");
table.Columns.Add("Model");
table.Columns.Add("Status", typeof(string)); //with type
There is no issue in biding.
Also you can project your required column to an Anonymous type and then bind that to your data grid like:
var result = ctx.ConsumerProducts
.Select(r=> new
{
Id = r.ID,
Model = r.Model,
Status = "Offline"
}).ToList();
dataGridView1.DataSource = result;
I am sorting my incoming files by my 'Submitted' column (which is a date/time stamp field), however I need it to be in descending order with the most recent file on top. From my research, it looked like I would simply do this:
view.Sort = "Submitted desc";
Is there something I'm missing? Here's the entire method:
private void PopulatePendingQueryGridView()
{
DataTable dt = new DataTable();
DataColumn col1 = new DataColumn("Spreadsheet", typeof(string));
dt.Columns.Add(col1);
DataColumn col2 = new DataColumn("Submitted", typeof(string));
dt.Columns.Add(col2);
string folder = Path.Combine(config.BulkQueryUploadFolder, CurrentUser);
if (Directory.Exists(folder))
{
string[] qryFiles = Directory.GetFiles(folder, "*.xlsx");
foreach (string qryFile in qryFiles)
{
FileInfo info = new FileInfo(qryFile);
DataRow row = dt.NewRow();
row["Spreadsheet"] = info.Name;
row["Submitted"] = info.CreationTime.ToString("yyyy/MM/dd HH:mm:ss");
dt.Rows.Add(row);
}
}
DataView view = new DataView(dt);
view.Sort = "Submitted desc";
pendingQryGridView.DataSource = view;
pendingQryGridView.DataBind();
gridUpdatePanel.Update();
}
Change your Submitted column data type to datetime. the sort direction shouldn't be case sensitive, but I'm not sure.
If needed you can set your datagrid field with a specified format.
how to bind the value to gridview
i have a datatable
DataTable dtBindGrid = new DataTable();
dtBindGrid = serviceobj.SelectExamTimeTable(txtSchoolName.Text, txtBranchName.Text, txtClass.Text, txtExamName.Text);
foreach (DataRow row in dtBindGrid.Rows)
{
strgetday= row["Day"].ToString();
strgetdate = row["Date"].ToString();
DatedTime = Convert.ToDateTime(row["Time"].ToString());
strgettime = DatedTime.ToString("t");
strgetsubject = row["Subject"].ToString();
strgetduration = row["Duration"].ToString();
strgetsession = row["Session"].ToString();
strgetminmark = row["MinMarks"].ToString();
strgetmaxmark = row["MaxMarks"].ToString();
// dtBindGrid.Rows.Add(row);
}
GrdExamTimeTable.DataSource = dtBindGrid.DefaultView;
GrdExamTimeTable.DataBind();
this datatable will return me some values like
day,date,time,duration,subject,..
here im getting each value in string bec to convert the Time as 9.00am or 9.00pm
DatedTime = Convert.ToDateTime(row["Time"].ToString());
strgettime = DatedTime.ToString("t");
.... how to bind this converted value to my gridview.
You could add an extra column to your DataTable once you've got it back from the service, then as you iterate through the rows, calculate the DatedTime and update the row. As it is then in your GridView's data source you should be able to bind it as normal through a BoundColumn.
DataTable dtBindGrid = new DataTable();
dtBindGrid = serviceobj.SelectExamTimeTable(...);
dtBindGrid.Columns.Add(new DataColumn("DatedTime"));
foreach (DataRow row in dtBindGrid.Rows)
{
DatedTime = Convert.ToDateTime(row["Time"].ToString());
strgettime = DatedTime.ToString("t");
row.BeginEdit();
row.SetField("DatedTime", strgettime);
row.EndEdit();
row.AcceptChanges();
}
GrdExamTimeTable.DataSource = dtBindGrid.DefaultView;
GrdExamTimeTable.DataBind();