Tag: datagridview

将Excel数据导入Visual Studio 2010中的DataGridView

请使用以下代码帮助修复从Excel文档导入数据到DataGridView控件: private void button5_Click(object sender, EventArgs e) { Excel.Application app = new Microsoft.Office.Interop.Excel.Application(); Excel.Workbook workbook =app.Workbooks.Open(@”C:\Users\Admin\Desktop\Dropbox\Vandit’s Folder\Internship\test.xlsx”); Excel.Worksheet worksheet = workbook.ActiveSheet; rcount = worksheet.UsedRange.Rows.Count; int i = 0; for(;i<rcount;i++) { dataGridView1.Rows[i].Cells["Column1"].Value = worksheet.Cells[i + 1, 1].Value; dataGridView1.Rows[i].Cells["Column2"].Value = worksheet.Cells[i + 1, 2].Value; } } 当我运行这段代码时,我总是得到一个例外 “Index was out of range. Must be non-negative and less […]

如何使用Datagridview绑定源C#更新SQL Server数据库

我正在用C#编写Winforms应用程序,使用户可以使用datagridview编辑和更新数据库。 问题是,我无法让它发挥作用。 我设法实现的唯一目标是更新datagridview显示的内容,但是当我进入数据库表时,数据没有变化。 我搜索了很多网站讨论这个问题,但没有什么对我有用。 到目前为止我尝试过的事情: 将数据库绑定到datagridview 将“ Copy to Output Directory属性更改为Copy if newer 使用dataAdapter.Update((DataTable)bindingSource1.DataSource)执行或不执行任何更新查询。 在没有dataAdapter.update(…)情况下执行更新查询。 这是我的代码: public Form1() { InitializeComponent(); GetData(“SELECT * FROM Table1”); } void GetData(string selectCommand) { SqlConnection conn = new SqlConnection(Properties.Settings.Default.NewDBTESTConnectionString); dataAdapter = new SqlDataAdapter(selectCommand, conn); commandBuilder = new SqlCommandBuilder(dataAdapter); table = new DataTable(); dataAdapter.Fill(table); bindingSource1.DataSource = table; dataGridView1.DataSource = bindingSource1; } […]

将两个datagridview列合并为一个新列

我想将两个datagridview列合并为一个新列。 我首先将两个col的Visible属性更改为false,然后我尝试添加新的col,该值必须格式化为col1Value和col2Value为以上列的值: string.Format(“{0} per {1}”, col1Value, col2Value); 我的代码 reportResultForm.dgvResult.Columns[“Height”].Visible = false; reportResultForm.dgvResult.Columns[“Width”].Visible = false; DataGridViewColumn col = new DataGridViewColumn(); col.DefaultCellStyle.Format = “{0} per {1}”; col.CellTemplate = new DataGridViewTextBoxCell(); dgvResult.Columns.Add(col); 但我不知道这是怎么回事! 请帮我。 我的方式是真的吗?

如何创建一个不将其行捕捉到控件顶部的DataGridView?

我们在客户端广泛使用了DataGridView。 我们的客户希望能够将大量文本输入到一行中 – 太多而不能在DataGridView的高度显示 – 并使用主滚动条滚动它们 – 即DataGridView的滚动条。 但是,DataGridView只会滚动,以便将行捕捉到控件的顶部; 如果一行大于DataGridView,那么你只能看到它的顶部。 如何创建一个不将其行捕捉到控件顶部的DataGridView?

WinForms:DataGridView – 程序化排序

我有一个带有datagridview的表单。 dataGridView绑定到BindingSource: public class Address { public string State { get; set; } public string City { get; set; } public string Street { get; set; } } this.addressBindingSource.DataSource = typeof(Address); this.dataGridView1.DataSource = this.addressBindingSource; 我像这样填写DataSource : addressBindingSource.DataSource = new BindingList { new Address {State = “S1”, City = “C1”, Street = “S1”}, new Address {State […]

如何禁用DataGridView的键盘shorcuts?

我刚刚注意到DataGridView有一个默认的快捷方式,这样无论何时按Ctrl + H , DataGridView的编辑控件退格,都可以删除单元格中的整个选择。 这可能会非常烦人,因为我想在按下Ctrl + H时打开一个替换框。 是否有任何方法可以停止退格,同时仍然可以使用它来打开替换盒? 我正在运行C#2.0,但如果较新的C#有解决方案,我可以将我的应用程序更新为3.5。

DataGridView设置行高不起作用

试图像这样设置RowHeight(在代码中): dgvTruckAvail.RowTemplate.Height = 48; 不行吗? 我也尝试设置我添加的每个列的高度 – 不起作用。 这是网格属性: this.dgvTruckAvail.AllowUserToAddRows = false; this.dgvTruckAvail.AllowUserToDeleteRows = false; this.dgvTruckAvail.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.dgvTruckAvail.BackgroundColor = System.Drawing.Color.White; this.dgvTruckAvail.BorderStyle = System.Windows.Forms.BorderStyle.None; this.dgvTruckAvail.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None; this.dgvTruckAvail.Columns.AddRange( new System.Windows.Forms.DataGridViewColumn[] { this.colMon, this.colTue, this.colWED, this.colThu, this.colFri, this.colSat, this.colSun}); this.dgvTruckAvail.Cursor = System.Windows.Forms.Cursors.Default; dataGridViewCellStyle8.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; dataGridViewCellStyle8.BackColor = System.Drawing.SystemColors.Window; dataGridViewCellStyle8.Font = […]

如何强制刷新DataGridView的内容?

我想做一个有序的datagridview输入。 以下代码片段并没有完全消除它; 即使我放了一个grd.Refresh,datagridview也不会显示更新的值。 如果我按下向下箭头并再次向上移动,则网格会更新。 有没有其他方法可以强制刷新datagridview的内容? using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace TestSortedInput { public partial class Form1 : Form { DataTable _dt = new DataTable(); public Form1() { InitializeComponent(); grd.AllowUserToAddRows = false; _dt.Columns.Add(“sort”, typeof(int)); _dt.Columns.Add(“product”, typeof(string)); _dt.DefaultView.Sort = “sort”; grd.DataSource = _dt; } private void dataGridView1_KeyUp(object […]

数据绑定后如何将图像设置为数据网格视图?

我在数据绑定后向DGV单元添加图像时遇到问题。 这是我的代码: DataTable tab = conn.searchData(searchTmp); bindingSource1.DataSource = tab; DGV.AllowUserToAddRows = false; DGV.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader); //dont show this colls DGV.Columns[“zId”].Visible = false; DGV.Columns[“tId”].Visible = false; DGV.Columns[“tId2”].Visible = false; DataGridViewImageColumn co = new DataGridViewImageColumn(); System.Reflection.Assembly thisExe; thisExe = System.Reflection.Assembly.GetExecutingAssembly(); System.IO.Stream file = thisExe.GetManifestResourceStream(“MyApp.Resources.Tx1[k].gif”); System.IO.Stream file2 = thisExe.GetManifestResourceStream(“MyApp.Resources.Tx2[k].gif”); //和其他所有列 – 第一个灰色行 Bitmap bmp = new Bitmap(file); co.Image = bmp; […]

如何使用DataGridView显示此数组的内容?

我创建了一个二维字符串数组并填充它。 我尝试将它绑定到DataGrid控件,如下所示: string[][] Array = new string[100][]; dataGridView.DataSource = Array; 我没有看到数组的内容,而是看到以下列:Length,LongLenth,Rank,SyncRoot,IsReadOnly,IsFixedSize,IsSyncrhonized。 因此,它不显示我的数组的内容,而是显示数组的属性。 我做错了什么?