I have an Access table which I want to display on a DataGridView, but the GridView doesn't display the data.
All the other functions inside this one works properly.
This is the function:
private void ShowSuppliersFrm_Load(object sender, EventArgs e)
{
dgvShowSupps.RowCount = dataB.GetSuppliersNumber();
dgvShowSupps.ColumnCount = 3;
dgvShowSupps.Columns[0].HeaderText = "SuppNumber";
dgvShowSupps.Columns[1].HeaderText = "SuppName";
dgvShowSupps.Columns[2].HeaderText = "SuppPhone";
Supplier[] supplierList = dataB.GetSupplierData();
int size = supplierList.Length;
for (int i = 0; i < size; i++)
{
dgvShowSupps[0, i].Value = supplierList[i].SuppNumber;
dgvShowSupps[1, i].Value = supplierList[i].SuppName;
dgvShowSupps[2, i].Value = supplierList[i].SuppPhone;
}
}
This is my table:
Normally with a datagridview, you would simply do this:
private void ShowSuppliersFrm_Load(object sender, EventArgs e)
{
var binding = new BindingSource();
binding.DataSource = dataB.GetSupplierData();
dgvShowSupps.DataSource = binding;
}
It can be a DataTable or a list of objects. Though you may have to do some data marshalling or setup on the DataGridView if it shows data that you don't want.
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource(v=vs.110).aspx
Related
I have the following DataSet:
The Product and Part tables can be edited using these DataGridViews:
When the user double-clicks a row in the Products grid, the following form opens:
The left column is supposed to list the parts associated with this product. The right column is supposed to list all the other parts. Using the << and >> buttons, the user should be able to choose which parts belong to the current product.
I have done something similar with a one-to-many relation and it worked perfectly. The code was as follows:
public partial class ProductPartsForm : Form
{
private int _productID;
private DataSet1 _data;
public ProductPartsForm(DataSet1 data, DataRowView productRowView)
{
var productRow = (DataSet1.ProductRow)productRowView.Row;
_productID = productRow.ID;
_data = data;
InitializeComponent();
productBindingSource.DataSource = productRowView;
assignedPartBindingSource.DataSource = productBindingSource;
assignedPartBindingSource.DataMember = "FK_Product_Part";
assignedPartsListBox.DisplayMember = "Name";
unassignedPartBindingSource.DataSource = _data;
unassignedPartBindingSource.DataMember = "Part";
unassignedPartsListBox.DisplayMember = "Name";
unassignedPartBindingSource.Filter = $"isnull(ProductID, 0) = 0";
}
private void assignButton_Click(object sender, EventArgs e)
{
var partRowView = (DataRowView)unassignedPartBindingSource.Current;
var partRow = (DataSet1.PartRow)partRowView.Row;
var productRowView = (DataRowView)productBindingSource.Current;
var productRow = (DataSet1.ProductRow)productRowView.Row;
partRow.ProductRow = productRow;
UpdateUI();
}
private void unassignButton_Click(object sender, EventArgs e)
{
var partRowView = (DataRowView)assignedPartBindingSource.Current;
var partRow = (DataSet1.PartRow)partRowView.Row;
partRow.SetProductIDNull();
UpdateUI();
}
private void UpdateUI()
{
assignedPartsListBox.Refresh();
unassignedPartsListBox.Refresh();
assignButton.Enabled = unassignedPartsListBox.Items.Count > 0;
unassignButton.Enabled = assignedPartsListBox.Items.Count > 0;
}
}
With the many-to-many relation, there are two things I couldn't get to work:
The left column doesn't show the names of the parts. It should display lowercase letters, like the right column; instead, it shows the string System.Data.DataRowView. I want to fix this using some sort of lookup, but I don't know how.
When you press <<, the selected part stays on the right column instead of moving to the left column. If you try to press << again with the same part, you get the following error:
System.Data.ConstraintException: 'Column 'ProductID, PartID' is constrained to be unique. Value '-4, -3' is already present.'
(which is understandable). I think this can be fixed using a filter expression, but I'm not sure how to write it and how to update the right column automatically after every change.
Has anyone done something similar and can help point me in the right direction?
Here's what I finally came up with. The key function is UpdateFilters, which creates a list of part IDs assigned to the current product and then filters the two columns "manually" using the IN and NOT IN operators.
public partial class ProductPartsForm : Form
{
private int _productID;
private DataSet1 _data;
public ProductPartsForm(DataSet1 data, DataRowView productRowView)
{
var productRow = (DataSet1.ProductRow)productRowView.Row;
_productID = productRow.ID;
_data = data;
InitializeComponent();
productBindingSource.DataSource = productRowView;
assignedPartBindingSource.DataSource = _data;
assignedPartBindingSource.DataMember = "Part";
assignedPartsListBox.DisplayMember = "Name";
unassignedPartBindingSource.DataSource = _data;
unassignedPartBindingSource.DataMember = "Part";
unassignedPartsListBox.DisplayMember = "Name";
}
private void ProductPartsForm_Load(object sender, EventArgs e)
{
UpdateFilters();
UpdateUI();
}
private void assignButton_Click(object sender, EventArgs e)
{
var partRowView = (DataRowView)unassignedPartBindingSource.Current;
var partRow = (DataSet1.PartRow)partRowView.Row;
var productRowView = (DataRowView)productBindingSource.Current;
var productRow = (DataSet1.ProductRow)productRowView.Row;
_data.ProductPart.AddProductPartRow(productRow, partRow);
UpdateFilters();
UpdateUI();
}
private void unassignButton_Click(object sender, EventArgs e)
{
var partRowView = (DataRowView)assignedPartBindingSource.Current;
var partRow = (DataSet1.PartRow)partRowView.Row;
var productPartRow = _data.ProductPart
.Single(pp => pp.ProductID == _productID && pp.PartID == partRow.ID);
_data.ProductPart.RemoveProductPartRow(productPartRow);
UpdateFilters();
UpdateUI();
}
private void UpdateFilters()
{
var assignedIds = _data.ProductPart
.Where(pp => pp.ProductID == _productID)
.Select(pp => pp.PartID.ToString());
if (assignedIds.Any())
{
assignedPartBindingSource.Filter = $"ID IN ({string.Join(",", assignedIds)})";
unassignedPartBindingSource.Filter = $"ID NOT IN ({string.Join(",", assignedIds)})";
}
else
{
assignedPartBindingSource.Filter = "FALSE";
unassignedPartBindingSource.RemoveFilter();
}
}
private void UpdateUI()
{
assignedPartsListBox.Refresh();
unassignedPartsListBox.Refresh();
assignButton.Enabled = unassignedPartsListBox.Items.Count > 0;
unassignButton.Enabled = assignedPartsListBox.Items.Count > 0;
}
}
I have a list attached to the datasource of a gridview, when I make the change from my list in the status field I would like to change the status in gridview as well.
My loop is through the datagridview and it is linked in my list, I think the problem is this?
How can I update my List loop in my DataGridView?
MyList
List<Dados> SendMsg;
Populating Form_Load
SendMsg = GetDados();
gvSent.DataSource = SendMsg;
Event Click
private void btnSend_Click(object sender, EventArgs e)
{
for (int i = gvSent.Rows.Count - 1; i >= 0; i--)
{
if (gvSent.Rows[i].Cells[0].Value != null)
{
//defaultDGV.Rows.RemoveAt(i);
long str = unchecked((long)gvSent.Rows[i].Cells[0].Value);
var query = (from send in SendMsg
where send.MessageSentId == str
select send)
.Update(st => { st.Status = "S"; });
MessageBox.Show(str.ToString());
gvSent.DataSource = query;
}
}
use a BindingSource
BindingSource bs = new BindingSource();
bs.DataSource = SendMsg;
yourDataGridView.DataSource = bs;
This is the error, how can I correct it. Anyone ..help
I want to arrange column names in Gridview, I wrote the code as C#, but it didn't work.
namespace ip_web
{
public partial class login : System.Web.UI.Page
{
private void dgv1()
{
GridView1.Columns["no"].DisplayIndex = 0;
GridView1.Columns["batch"].DisplayIndex = 1;
GridView1.Columns["degree"].DisplayIndex = 2;
GridView1.Columns["module"].DisplayIndex = 3;
// dataGridView1.Columns["lecturer/instructor"].DisplayIndex = 4;
GridView1.Columns["date"].DisplayIndex = 5;
GridView1.Columns["time"].DisplayIndex = 6;
}
protected void Page_Load(object sender, EventArgs e)
{
Service1Client com = new Service1Client();
GridView1.DataSource = com.GetComTimeTable();
GridView1.DataBind();
}
}
}
According to your tag asp.net and your example source, you have used GridView. Unfortunately there is not any property DisplayIndex in GridView. Please check GridView Class.
The property DisplayIndex exists in DataGridView, it's a WinForms control. Please check DataGridView Class. And DataGridView allow to get a column by using name and index both. Please check below image;
private void dgv1()//show patient table
{
dataGridView1.Columns["id"].DisplayIndex = 0;
dataGridView1.Columns["name"].DisplayIndex = 1;
dataGridView1.Columns["gender"].DisplayIndex = 2;
dataGridView1.Columns["age"].DisplayIndex = 3;
dataGridView1.Columns["phone"].DisplayIndex = 4;
}
public void patientTableView()
{ //add to dataGridView
Service1Client ptn = new Service1Client();
dataGridView1.DataSource = ptn.GetpatientTable();
}
private void Form1_Load(object sender, EventArgs e)
{//call the method when form loading
patientTableView();
}
On Button click event I execute a new query i get a new table but i am unable to populate it on to the same grid view. Is there any why where i can use schema instead of dataTables.
private void GetData()//This method Displays the Datatable onto the Grid
{
int intCount = 0;
int.TryParse(txt_messagecount.Text, out intCount);
DataTable dtData = null;
//dtData = _objIMRSData.GetData(txt_fromdate.Text, txt_todata.Text, txt_messagecount.Text);
dtData = _objIMRSData.GetTransactionData( txt_fromdate.Text, txt_todata.Text,intCount );
grd_transactionLog.DataSource = dtData;
grd_transactionLog.DataBind();
dtData.Clear();
}
//On Button click event I execute a new query i get a new table
//but i am unable to populate it on to the same grid view
protected void btn_next_Click(object sender, EventArgs e)
{
int messageCount = int.Parse(txt_messagecount.Text);
string lastRecord = grd_transactionLog.Rows[messageCount-1].Cells[1].Text;
DataTable dtData1 = null;
//dtData = _objIMRSData.GetData(lastRecord, txt_todata.Text, txt_messagecount.Text);
dtData1 = _objIMRSData.GetTransactionData(lastRecord, txt_todata.Text, messageCount);
//grd_transactionLog.Columns.Clear();
grd_transactionLog.DataSource = dtData1;
grd_transactionLog.DataBind();
dtData1.Clear();
}
Reassign the datasource. In the click event do this.
grd_transactionLog.DataSource = null;
grd_transactionLog.DataSource = dtData1;
My goal is to group the data at runtime inside a grdView which added to a panel also at runtime
grdView.DataSource = tbl;
grdView.DataBind();
grdView.Settings.ShowGroupPanel = true;
grdView.BeginUpdate();
grdView.GroupBy((DevExpress.Web.ASPxGridView.GridViewDataColumn) grdView.Columns["ClmnName"]);//or an index (0) for example
grdView.EndUpdate();
any suggestions?
EDIT:
Current Code
//GRID
pnlGrids.Controls.Add(grdView);
grdView.DataSource = tbl;//Datasource
foreach (GridViewDataTextColumn clmn in grdView.Columns)//HTML
clmn.PropertiesTextEdit.EncodeHtml = false;
if (key.GroupingDataMembers.Any())//Group panel
grdView.Settings.ShowGroupPanel = true;
grdView.Images.ImageFolder = "~/App_Themes/Aqua/GridView/";//Style
grdView.Styles.CssFilePath = "~/App_Themes/Aqua/GridView/styles.css";
grdView.Styles.CssPostfix = "Aqua";
grdView.DataBind();//Bind
if (key.GroupingDataMembers.Any())//Grouping
(grdView.Columns[key.GroupingDataMembers.First().DataMember.DisplayName] as DevExpress.Web.ASPxGridView.GridViewDataColumn).GroupBy();
grdView.ExpandAll();//Expand all
The following code works fine here:
protected void Page_Load(object sender, EventArgs e) {
ASPxGridView grid = new ASPxGridView();
grid.ID = "grid";
pnl.Controls.Add(grid);
DataTable t = new DataTable();
t.Columns.Add("Id");
t.Columns.Add("Data");
for(int i = 0; i < 100; i++) {
t.Rows.Add(new object[] { i, "row " + i.ToString() });
}
grid.DataSource = t;
grid.Settings.ShowGroupPanel = true;
grid.DataBind();
(grid.Columns["Data"] as GridViewDataColumn).GroupBy();
}