我有一个带有DataGridView的应用程序,我想定位行,使特定的行位于列表的顶部.
我不想要排序,我想要一种以编程方式告诉DataGridView"滚动到第N行"的方法.
有任何想法吗?
我希望当用户点击一个单元格时出现一个OpenFileDialog,然后在单元格中显示结果.
这一切都有效,除了DataGridView显示一个额外的行,用于将值添加到它所绑定的列表中.该行显示是否dataGridView.AllowUserToAddNewRows == true,这就是我想要的.我不想要的是当以编程方式编辑该行时应用程序崩溃; 相反,它应该完全按照用户手动编辑该行的方式执行(将新行添加到基础列表,将另一个空行推入网格以添加值).
我读到了SendKeys.Send(),它应该使DataGridView的行为与用户输入的值完全相同; 但是,它也不起作用.这是我正在尝试的:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
dataGridView1.CurrentCell = cell;
//simply doing a cell.Value = etc. will cause the program to crash
cell.ReadOnly = false;
dataGridView1.Columns[cell.ColumnIndex].ReadOnly = false;
dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
dataGridView1.BeginEdit(true);
SendKeys.Send(openFileDialog1.FileName + "{Enter}");
dataGridView1.EndEdit();
cell.ReadOnly = true;
dataGridView1.Columns[cell.ColumnIndex].ReadOnly = true;
}
//I would expect the FileName would be in the cell now, and a new empty
//row tacked onto the end of the DataGridView, but it's not; the DataGridView
//is not …Run Code Online (Sandbox Code Playgroud) 所以我用我的DGV填充了一些数据,并将一些列设置为不可见:
var part = inventory.espiromex_product.Where(p => p.descriptionsmall == cmbMainP.Text).First().partnumberp;
dtgAssy.DataSource = inventory.espiromex_productsub.Where(p => p.partnumberp == part);
dtgAssy.Columns["idproductsub"].Visible = false;
dtgAssy.Columns["partnumberp"].Visible = false;
dtgAssy.Columns["partnumbersubp"].Visible = true;
dtgAssy.Columns["quantity"].Visible = true;
dtgAssy.Columns["comments"].Visible = true;
dtgAssy.Columns["assemblyno"].Visible = false;
dtgAssy.Columns["assemblynodesc"].Visible = false;
dtgAssy.Columns["uomid"].Visible = true;
dtgAssy.Columns["subassemblylevelnumber"].Visible = false;
dtgAssy.Columns["scrappercent"].Visible = true;
Run Code Online (Sandbox Code Playgroud)
这很好,但列按字母顺序排序,如何以编程方式重新排序列?
请注意,库存是一个Entitie,我正在使用Linq to Entities.
我有一个带有DataGridView小部件的表单,我需要获取具有所选名称的列的索引.
例如,假设我有一个包含2列的表:Name,Surname.我需要一种方法来获取列名的索引.问题是它一直在变化,具体取决于DataSource但该列始终具有相同的名称"Name".
有谁知道如何解决这个问题?
我正在开发一个应用程序,它将我的DataGridView名为scannerDataGridView导出到csv文件.
找到一些示例代码来执行此操作,但无法使其正常工作.顺便说一下,我的数据网格没有数据绑定到源.
当我尝试使用Streamwriter只编写列标题时一切顺利,但当我尝试导出整个数据网格包括数据时,我得到一个exeption trhown.
System.NullReferenceException:未将对象引用设置为对象的实例.在Scanmonitor.Form1.button1_Click(Object sender,EventArgs e)
这是我的代码,错误在以下行中给出:
dataFromGrid = dataFromGrid +','+ dataRowObject.Cells [i] .Value.ToString();
//csvFileWriter = StreamWriter
//scannerDataGridView = DataGridView
private void button1_Click(object sender, EventArgs e)
{
string CsvFpath = @"C:\scanner\CSV-EXPORT.csv";
try
{
System.IO.StreamWriter csvFileWriter = new StreamWriter(CsvFpath, false);
string columnHeaderText = "";
int countColumn = scannerDataGridView.ColumnCount - 1;
if (countColumn >= 0)
{
columnHeaderText = scannerDataGridView.Columns[0].HeaderText;
}
for (int i = 1; i <= countColumn; i++)
{
columnHeaderText = columnHeaderText + ',' + scannerDataGridView.Columns[i].HeaderText;
}
csvFileWriter.WriteLine(columnHeaderText);
foreach (DataGridViewRow …Run Code Online (Sandbox Code Playgroud) 我有一个DataGridView,我需要添加自定义对象.请考虑以下代码:
DataGridView grid = new DataGridView();
grid.DataSource = objects;
Run Code Online (Sandbox Code Playgroud)
使用此代码,我得到一个DataGridView对象,其所有属性都是列.就我而言,我不想展示所有这些信息; 我想只展示两三列.我知道我可以设置
AutoGenerateColumns = false.
但我不知道如何继续进行.一种选择是隐藏所有我不感兴趣的列,但我认为最好以相反的方式进行.我怎样才能做到这一点?
如何循环每行readed?在我的代码中,由于产品ID相同,行不会绑定到下一行,因此datagridview不会移动到新行,它仍然在同一行并覆盖价格(对于某些产品,我有两个价格) .如何循环它以显示相同的productID但它有不同的价格.
EX:1汉堡包含2个价格汉堡包:1美元和2美元,当我得到数据循环它,重新应该有2行相同的产品,但不同的价格.这该怎么做?下面是我的代码
productID = odr["product_id"].ToString();
quantity = Double.Parse(odr["quantity"].ToString());
//processing data...
key = productID.ToString();
if (key != key_tmp)
{
//update index...
i++;
//updating variable(s)...
key_tmp = key;
}
if (datagridviews.Rows[i].Cells["qty"].Value != null) //already has value...
{
currJlh = Double.Parse(ddatagridviews.Rows[i].Cells["qty"].Value.ToString());
}
else //not yet has value...
{
currQty = 0;
}
currQty += qty;
//show data...
datagridviews.Rows[i].Cells["price"].Value = cv.toAccountingCurrency_en(Double.Parse(odr["item_price"].ToString()));
MessageBoxes.Show(i.ToString());
MessageBoxes.Show(datagridviews.Rows[i].Cells["price"].Value.ToString()); // in here there is two price that looped but won't showed in datagridviews
Run Code Online (Sandbox Code Playgroud)
帮帮我 :)
在Windows窗体中,我试图DataGridView通过插入DataGridViewRows来手动填充,所以我的代码如下所示:
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dgvArticles);
row.Cells[0].Value = product.Id;
row.Cells[1].Value = product.Description;
.
.
.
dgvArticles.Rows.Add(row);
Run Code Online (Sandbox Code Playgroud)
但是,我想按列名添加Cell值,而不是通过索引来添加它,如下所示:
row.Cells["code"].Value = product.Id;
row.Cells["description"].Value = product.Description;
Run Code Online (Sandbox Code Playgroud)
但这样做会抛出一个错误,说它无法找到名为"code"的列.我正在设计器中设置DataGridView列,如下所示:

难道我做错了什么?我怎样才能完成我想做的事情?
我有一个带有datagridview的win表单(c#).我将网格的数据源设置为数据表.
用户想要检查数据表中的某些数据是否存在于另一个源中,因此我们遍历表将行与其他源进行比较,并将数据表上的rowerror设置为短消息.datagridview未显示这些错误.datagridviewrows上的errortext已设置,但未显示错误.
我只是期望显示错误太多,它们只显示在编辑网格中的数据的上下文中?
我一直在修补这一天,并寻找一个发布simalar问题的人无济于事 - 帮助!
我在datagridview中有数字的字符串列.它没有绑定,我想按照我使用的数字排序
colid.ValueType = typeof(int);
grid.Sort(colid, ListSortDirection.Descending);
Run Code Online (Sandbox Code Playgroud)
但是像字符串一样排序,例如:
11
12
23
7
80
81
Run Code Online (Sandbox Code Playgroud)
而预期的是
7
11
12
23
80
81
Run Code Online (Sandbox Code Playgroud) c# ×10
datagridview ×10
winforms ×4
.net ×2
data-binding ×1
indexing ×1
loops ×1
sorting ×1
streamwriter ×1