我正在尝试在C#WinForm中滚动到DataGridView的底部.
此代码适用于TextBox:
textbox_txt.SelectionStart = textbox_txt.Text.Length;
textbox_txt.ScrollToCaret();
Run Code Online (Sandbox Code Playgroud)
...但我不知道如何使用DataGridView.有什么帮助吗?
如何在Datagridview C#中为特定列设置列标题文本
我一直在尝试将一个Entity Framework对象绑定到一个DataGridView,但我一直在追求死胡同,我似乎无法在任何地方找到我的答案.
我可以将整个表(实体)绑定到gridview,它将允许我进行更改并将这些更改保存回DB,如下所示:
WS_Model.WS_Entities context;
private void simpleButton1_Click(object sender, EventArgs e)
{
context = new WS_Entities();
var query = from c in context.Users select c;
var users = query.ToList();
gridControl1.DataSource = users;
}
private void simpleButton2_Click(object sender, EventArgs e)
{
context.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)
但我不想在我的数据库视图中查看数据库中表格中的所有列,所以我尝试这样做...
WS_Entities context = new WS_Entities();
private void simpleButton1_Click(object sender, EventArgs e)
{
var query = from c in context.Users
where c.UserName == "James"
select new { c.UserName, c.Password, c.Description };
var results = query.ToList();
gridControl1.DataSource = results; …Run Code Online (Sandbox Code Playgroud) 我正在使用数据绑定Windows窗体DataGridView.如何从用户选择的行走在DataGridView到DataRow的DataTable是它的来源?
我有一个Dictionary包含物品和价格.这些项目是唯一的,但在应用程序的生命周期中慢慢添加和更新(也就是说,我事先不知道项目字符串).我想将此结构绑定到DataGridView,因此我可以在我的表单上显示更新,如:
Dictionary<string, double> _priceData = new Dictionary<string, double>();
BindingSource _bindingSource = new BindingSource();
dataGridView1.DataSource = _bindingSource;
_bindingSource.DataSource = _priceData;
Run Code Online (Sandbox Code Playgroud)
但不能,因为Dictionary没有实现IList(或IListSource,IBindingList或IBindingListView).
有没有办法实现这个目标?我需要保留一个唯一的项目列表,但也要更新现有项目的价格,因此Dictionary我认为a 是理想的数据结构,但我找不到在我的表单上显示数据的方法.
更新:
Marc的建议下面的工作非常好,但我仍然不确定如何在执行期间更新DataGridView.
我有一个类级变量:
private DictionaryBindingList<string, decimal> bList;
Run Code Online (Sandbox Code Playgroud)
然后在Main():中实例化
bList = new DictionaryBindingList<string,decimal>(prices);
dgv.DataSource = bList;
Run Code Online (Sandbox Code Playgroud)
然后在程序执行期间,如果将新条目添加到字典中:
prices.Add("foobar", 234.56M); bList.ResetBindings();
Run Code Online (Sandbox Code Playgroud)
我认为这将刷新DataGridView.为什么不?
我有一个带有复选框列的网格视图,我想在切换单元格的值后立即触发绘图事件.我尝试了ValueChaged和CellEndEdit以及BeginEdit,并选择了选择模式作为CellSelect.至于前2个事件,事件是在编辑模式结束时触发的,例如移出当前单元格或来回移动.这只是一种奇怪的行为.
一旦单元格值发生变化,是否有任何东西在网格视图上触发事件?
最好的祝福,
DataGridView控件是否可以在单元格中显示多行文本?
我正在使用Visual Studio 2005和C#.
我希望我的datagrid只允许选择一行.
我想我可以通过向datagridviews selectionChanged-event添加代码来实现这一点,但是有更好的解决方案吗?
我宁愿只设置一个属性,即: mydatagridview.maximumNrOfRowsSelected = 1; 或类似的东西.
有这样的事吗?
我想只启用DataGridview中的两列才能进行编辑.其他人不应该被允许编辑.此外,我没有直接链接到数据源; 我会做这样的事情
DataTable dt = new DataTable();
dt.Columns.Add("Email");
dt.Columns.Add("email1");
for (int i = 0; i < 5; i++)
{
DataRow dr = dt.NewRow();
dr["Email"] = i.ToString();
dr["email1"] = i.ToString() + "sdf";
dt.Rows.Add(dr);
}
BindingSource bs = new BindingSource();
bs.DataSource = dt;
dataGridView1.DataSource = bs;
Run Code Online (Sandbox Code Playgroud)
那么我应该设置哪个属性,这将只允许一列说电子邮件(在上面的例子中)是可编辑的.
我已经从配置文件中将设置名称及其各自的值提取到有序字典中.字典包含ICollection类的键和值.我想绑定该数据并将其显示在DataGridView中.我已经尝试将字符串复制到数组并显示这些数组,但是当我运行程序时,列是空白的,它似乎根本没有绑定.
我还尝试将DataGridView源直接设置为有序字典集合(键或值),但这也没有产生我想要的任何东西; 列仍然是空白的.但是,第三列的列名称为"length",并显示ICollection中条目的长度.但不用说我不想要长度,我想要条目本身.
这是我用于此问题的代码:在加载表单时,我加载配置文件,名为m_Settings的私有成员具有所有键值对.然后我创建一个列表并分别添加键和值.将绑定源设置为'data'后,我运行程序,我添加的两个列都是空白的.
private void Form4_Load(object sender, EventArgs e)
{
loadconfigfile(Properties.Settings.Default.Config);
List<Object> data = new List<Object>();
data.Add(m_Settings.Keys);
data.Add(m_Settings.Values);
bindingSource1.DataSource = data;
dataGridView1.DataSource = bindingSource1;
dataGridView1.Refresh();
}
Run Code Online (Sandbox Code Playgroud)
关于如何获得有序字典并在标有"设置"和"值"的两列中显示条目的任何想法?我相信列表是兼容DataGridViews的DataSources,但现在我开始猜测自己了.
任何帮助或方向非常感谢!如果需要,我很乐意提供更多信息.
谢谢!
编辑:
以下是已实现的myStruct类的修订代码:
List<myStruct> list = new List<myStruct>();
for(int index = 0; index < m_Settings.Count; index++)
{
myStruct pair = new myStruct(keys[index], values[index].ToString());
list.Add(pair);
}
public class myStruct
{
private string Key { get; set; }
private string Value { get; set; }
public myStruct(string key, string value)
{
Key …Run Code Online (Sandbox Code Playgroud) datagridview ×10
c# ×9
winforms ×9
.net ×3
c#-2.0 ×1
checkbox ×1
data-binding ×1
datasource ×1
datatable ×1
dictionary ×1
events ×1
icollection ×1
scrollbar ×1
select ×1