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();
Related
I need some assistance in the best way to edit my DataTable to handle possible null values.
In this instance their is potential for any of the items from the list being added to the DataTable to be null. I'm looking for the best way to account for those null values when creating the DataTable so when I insert this into MSSQL I don't run into errors.
Here is the code I'm using to generate the DataTable:
public DataTable ConvertListToCustomDataTable(List<RootObject> listOfItems)
{
DataTable table = new DataTable();
table.Columns.Add("DateCreated");
table.Columns.Add("DepthCode");
table.Columns.Add("DepthDateCreated");
table.Columns.Add("DepthLevel");
table.Columns.Add("DepthID");
table.Columns.Add("DepthCategoryName");
table.Columns.Add("DepthName");
table.Columns.Add("DepthCategoryDateUpdated");
table.Columns.Add("DepthDateUpdated");
table.Columns.Add("Name");
table.Columns.Add("ID");
table.Columns.Add("CategoryCode");
table.Columns.Add("CategoryDateCreated");
table.Columns.Add("CategoryDateUpdated");
table.Columns.Add("CategoryID");
table.Columns.Add("CategoryName");
table.Columns.Add("Code");
foreach (var item in listOfItems)
{
var row = table.NewRow();
row["DateCreated"] = item.DateCreated;
row["DepthCode"] = item.Depth.Code;
row["DepthDateCreated"] = item.Depth.DateCreated;
row["DepthLevel"] = item.Depth.Level;
row["DepthID"] = item.Depth.ID;
row["DepthCategoryName"] = item.Depth.Category.Name;
row["DepthName"] = item.Depth.Name;
row["DepthCategoryDateUpdated"] = item.Depth.Category.DateUpdated;
row["DepthDateUpdated"] = item.Depth.DateUpdated;
row["Name"] = item.Name;
row["ID"] = item.ID;
row["CategoryCode"] = item.Category.Code;
row["CategoryDateCreated"] = item.Category.DateCreated;
row["CategoryDateUpdated"] = item.Category.DateUpdated;
row["CategoryID"] = item.Category.ID;
row["CategoryName"] = item.Category.Name;
row["Code"] = item.Code;
table.Rows.Add(row);
}
return table;
}
You need to set the table columns to accept nulls.
DataColumn datecolumn = new DataColumn("DateCreated");
datecolumn.AllowDBNull = true;
I would put the Column names in an array and loop through them, ie something similar to this.
DataTable table = new DataTable();
string[] column = { "DateCreated", "DepthCode", "DepthDateCreated" };
foreach (var item in column)
{
DataColumn datecolumn = new DataColumn(item);
datecolumn.AllowDBNull = true;
table.Columns.Add(item);
}
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 wanna know how I can transform this to GridControl code instead of DataGridView.
foreach (DataGridViewRow row in (IEnumerable)this.dataGridView1.Rows)
{
Data.SomethingA item = new Data.SomethingA
{
item.ac = Convert.ToUInt32(row.Cells[5].Value.ToString())
};
item.ad = Convert.ToUInt32(row.Cells[2].Value.ToString()[7].ToString());
item.ab = row.Cells[1].Value.ToString();
item.az = row.Cells[3].Value.ToString();
item.ae = Convert.ToUInt32(row.Cells[4].Value.ToString());
item.aq = row.Cells[6].Value.ToString();
ABC.Add(item);
}
Thank you
I assume you're using DataTable as DataSource. Cast it back to DataTable and loop through rows of datatable
private void DoSomething()
{
DataTable table = (DataTable)grid.DataSource;
foreach (DataRow row in table.Rows)
{
Data.SomethingA item = new Data.SomethingA
{
item.ac = Convert.ToUInt32(row[5].ToString())
};
item.ad = Convert.ToUInt32(row[2].ToString()[7].ToString());
item.ab = row[1].ToString();
item.az = row[3].ToString();
item.ae = Convert.ToUInt32(row[4].ToString());
item.aq = row[6].ToString();
ABC.Add(item);
}
}
You can just set the AspxGridView.DataSource = to dataGridView1.DataSource, or even better just set the AspxGrid.DataSource to be whatever your underlying datasource is (DataTable etc.).
The AspxGrid has a property to Auto generate the columns from the datasource.
I have a ComboBox with its DataSource set to an instance of a DataTable. When rows are added to the DataTable, they show in the ComboBox without additional code, but when a row is deleted, the ComboBox remains unchanged. A short summary of my code:
ComboBox selector = new ComboBox();
DataTable tbl = new DataTable();
PopulateTable()
{
DataRow row1 = tbl.NewRow();
row1["field1"] = 1;
row1["field2"] = "Some Text";
tbl.Rows.Add(row1);
DataRow row2 = tbl.NewRow();
row2["field1"] = 2;
row2["field2"] = "More Text";
tbl.Rows.Add(row2);
}
PopulateSelector()
{
selector.DisplayMember = "field2";
selector.ValueMember = "field1";
selector.DataSource = tbl;
}
RemoveRow()
{
tbl.Rows[0].Delete();
}
At this point, the ComboBox appears to be correct, but clicking it resets it to its previous data. The DataTable remains correct, deleting the row causes no problem in that instance, I just can't make the ComboBox reflect the changes.
Try this:
PopulateSelector()
{
selector.DataSource = null;
selector.DisplayMember = "field2";
selector.ValueMember = "field1";
selector.DataSource = tbl;
}
RemoveRow()
{
tbl.Rows[0].Delete();
PopulateSelector()
}
You can use a bindingsource inbetween the datatable and the combobox and call ResetBindings
I have a DataView which has two columns: ContactID, and Name
How can I check if a particular ContactID is already existing in the DataView?
Have you had a look at DataView.FindRows Method or maybe DataView.Find Method
The DataView has a method called FindRows, this can be used to search for a specific contactID, for example...
var table = new DataTable();
var column = new DataColumn("Id", typeof (int));
table.Columns.Add(column);
table.PrimaryKey = new[] {column}; // Unique Constraint
var row = table.NewRow();
row["Id"] = 100;
table.Rows.Add(row);
row = table.NewRow();
row["Id"] = 200;
table.Rows.Add(row);
var view = new DataView(table) { ApplyDefaultSort = true };
var rows = view.FindRows(200);
foreach(var r in rows)
{
Console.WriteLine(r["Id"]);
}
Use the Following code to Find the row in the Dataview
//Your original Table Which consist of Data
DataTable dtProducts = new DataTable();
//Add the DataTable to DataView
DataView ProductDataView = new DataView(dtProducts);
ProductDataView.RowFilter = "";
ProductDataView.Sort = "ProdId";
int recordIndex = -1;
//In the Find Row Method pass the Column
//value which you want to find
recordIndex = ProductDataView.Find(1);
if (recordIndex > -1)
{
Console.WriteLine("Row Found");
}