C# DevExpress GridControl Rows - c#

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.

Related

Need help Creating a Datatable from a list with possible null values

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);
}

DataGridViewComboBoxColumn not showing DataSource

My code:
private void InitGrid(DataTable dataTable, ref DataGridView grid, bool resetColumns)
{
if (grid.Columns.Count > 0)
{
if (resetColumns)
{
grid.Columns.Clear();
grid.DataSource = null;
}
else
{
return;
}
}
DataGridViewColumn column;
grid.AutoGenerateColumns = false;
// apply entire data from table to grid.
grid.DataSource = dataTable;
// set column properties according to their type (combo, text, etc.)
foreach (DataColumn col in dataTable.Columns)
{
if (m_ColumnTypes[col.ColumnName].Equals("COMBO"))
{
// get query number to load specific column's data
string adminQuery = Server.LoadData("633", new List<string>() { col.ColumnName, m_TableFullName }).Rows[0][0].ToString();
column = new DataGridViewComboBoxColumn();
string queryParam = Controller.MainController.User;
switch (col.ColumnName.ToUpper())
{
case "FIELD1NAME":
(column as DataGridViewComboBoxColumn).DisplayMember = col.ColumnName;
(column as DataGridViewComboBoxColumn).ValueMember = "field1id";
break;
case "FIELD2NAME":
(column as DataGridViewComboBoxColumn).DisplayMember = col.ColumnName;
(column as DataGridViewComboBoxColumn).ValueMember = "field2id";
queryParam = string.Concat("select column_value from table(fn_mgr_workspaces('", Controller.MainController.User, "'))");
break;
case "PRIORITY":
(column as DataGridViewComboBoxColumn).DisplayMember = col.ColumnName;
(column as DataGridViewComboBoxColumn).ValueMember = col.ColumnName;
queryParam = "10";
break;
}
(column as DataGridViewComboBoxColumn).DataSource = Server.LoadData(adminQuery, new List<string>() { queryParam });
}
else
{
column = new DataGridViewTextBoxColumn() { AutoSizeMode = m_ColumnTypes[col.ColumnName].Equals("TAGS") ? DataGridViewAutoSizeColumnMode.Fill : DataGridViewAutoSizeColumnMode.AllCells, ReadOnly = true };
}
column.Name = col.ColumnName.ToUpper();
column.HeaderText = m_ColumnNameTranslations[col.ColumnName.ToUpper()];
column.DataPropertyName = column is DataGridViewComboBoxColumn ? (column as DataGridViewComboBoxColumn).ValueMember : col.ColumnName;
grid.Columns.Add(column);
}
// after setting columns. going over every row and trying to filter out the datasource of field2. this is where my problem is
foreach (DataGridViewRow dgvr in grid.Rows)
{
if (!dgvr.IsNewRow)
{
string wsId = dgvr.Cells["filed1id"].Value.ToString();
DataTable filteredData = ((dgvr.Cells["field2name"] as DataGridViewComboBoxCell).DataSource as DataTable).Select("thefieldname = " + wsId).CopyToDataTable();
DataView dv = filteredData.DefaultView;
dv.Sort = "field2name";
(dgvr.Cells["field2name"] as DataGridViewComboBoxCell).DataSource = null;
(dgvr.Cells["field2name"] as DataGridViewComboBoxCell).DisplayMember = "field2name";
(dgvr.Cells["field2name"] as DataGridViewComboBoxCell).ValueMember = "field2id";
(dgvr.Cells["field2name"] as DataGridViewComboBoxCell).DataSource = dv.ToTable();
}
}
}
(field names and some object names were scrambled for security purposes)
My problem:
The DataSource of field2 which is a ComboboxColumn does not change. It always shows me the entire data and not the filtered data that I try to set for it.
Even though I nullify it and re-set it to the filtered data, it doesn't work.
What I'm trying to achieve is that the DataSource in field2 column should be dependant on the data of field1 for each row of the grid.
I tried setting the datasource for the other columns and not setting it for field2 and then after that going over every row and setting the filtered datasource on field2 but it comes up completely empty as if the datasource set for every row hasn't worked at all.
I'm doing something wrong. How the hell do I do this?
The EditControlShowing event on the DataGridView will allow you to accomplish this.
Add an event handler to this method.
private void grid_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if ( grid.CurrentCell.ColumnIndex == dgvr.Columns["field2name"].Index )
{
// Get Combobox
ComboBox combo = e.Control as ComboBox;
// Get Other Column's Value
string wsId = grid.Rows[grid.CurrentCell.RowIndex].Cells["filed1id"].Value.ToString();
// Get Filtered Data Based Off Of Other Column's Value
DataTable filteredData = (combo.DataSource as DataTable).Select("thefieldname = " + wsId).CopyToDataTable();
DataView dv = filteredData.DefaultView;
dv.Sort = "field2name";
// Rebind
combo.DataSource = null;
combo.DisplayMember = "field2name";
combo.ValueMember = "field2id";
combo.DataSource = dv.ToTable();
}
}

Setting value of DataGridComboBoxColumn not reflected in the UI

I have a dataview that contains a DataGridComboBoxColumn for one value. I set the options with a list of strings, and then loop through the rows to set the initial values of this column. I can debug after looping through and the value is there as I expect it, but in the UI my combo box is still empty. Am I missing something that will set the value in the UI or what am I doing wrong?
private void PrepareList() {
var dt = new DataTable {
Columns = {
new DataColumn("Style Number"),
new DataColumn("Description"),
new DataColumn("Department"),
}
};
foreach (var orderEntity in _fullList) {
foreach (var line in orderEntity.Lines) {
dt.Rows.Add(line.VendorItemNumber, line.Description, line.Department);
}
}
var view = new DataView(dt) {Sort = "Style Number asc"};
var distinct = view.ToTable(true, "Style Number", "Description");
dataGrid.DataSource = distinct;
var repo = new DepartmentRepository();
var departments = repo.GetDepartmentList();
var vendor = new DataGridViewComboBoxColumn {
DataSource = departments,
ValueType = typeof(string),
HeaderText = #"Department",
Name = "Department",
DataPropertyName = "Department"
};
dataGrid.Columns.Add(vendor);
foreach (DataGridViewRow row in dataGrid.Rows) {
var dtRow = dt.AsEnumerable().FirstOrDefault(r =>
r["Style Number"].ToString() == row.Cells["Style Number"].Value.ToString());
var dept = dtRow == null ? "" : departments.FirstOrDefault(
s => s.StartsWith(dtRow["Department"].ToString()));
row.Cells["Department"].Value = dept;
}
dataGrid.Refresh();
foreach (DataGridViewColumn column in dataGrid.Columns) {
column.SortMode = DataGridViewColumnSortMode.NotSortable;
}
}
dataGrid.DataSource = distinct;
this code It should be placed after:
dataGrid.Columns.Add(vendor);
this way:
dataGrid.Columns.Add(vendor);
dataGrid.DataSource = null;
dataGrid.DataSource = distinct;
Edited
For me it works perfectly as follows:
DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn();
column.Name = "new";
column.HeaderText = "New";
column.DisplayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton;
column.DisplayIndex = 2;
datagridview1.Columns.Add(column);
column.DataSource = columnDataSource;
datagridview1.DataSource = null;
datagridview1.DataSource = dataGridViewDataSource;
datagridview1.Refresh();

Editing the DataSource of a ComboBox

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

how to bind the converted value from a datatable to my gridview

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();

Categories