C#2008 SP1.
我正在从datagridview上当前选中的行中删除一行.
我正在使用Typed数据集,我的datagridview被绑定到绑定源.
但是,我认为我的技术并不是最好的,即使它有效.
非常感谢任何建议,
DataRow[] drDelete;
// Get the value of the PK from the currently selected row
DataRowView drv = (DataRowView)bsAddressBook.Current;
int index = Convert.ToInt16(drv[0]);
// Find the actual data row from the primary key and delete the row
drDelete = dsCATDialer.AddressBook.Select(string.Format("ID = '{0}'", index));
dsCATDialer.AddressBook.Rows.Remove(drDelete[0]);
Run Code Online (Sandbox Code Playgroud) 如何使用代码更新绑定源中的列值?
我正在尝试类似的东西:
CustomersBindingSource.AddNew();
CustomersBindingSource.Current["CustomerID"] = Guid.NewGuid();
Run Code Online (Sandbox Code Playgroud)
此代码当前错误说明:"无法将带有[]的索引应用于'object'类型的表达式".
任何帮助重写这一点非常感谢!
你好我不能按字母顺序排序我的datagridview
这就是我填充网格的方式:
bs = new BindingSource();
bs.DataSource = db.GetProducts.ToList();
dgvInventory.DataSource = bs;
Run Code Online (Sandbox Code Playgroud)
这就是我尝试排序网格的方式:
private void toolStripButton3_Click_1(object sender, EventArgs e)
{
bs.Sort = "ID DESC, Name ASC";
dgvInventory.DataSource = bs;
}
Run Code Online (Sandbox Code Playgroud)
当我按下按钮没有任何反应这两列是数据网格中的exsist
这是屏幕:

我无法获取当前行值。我能怎么做?
Run Code Online (Sandbox Code Playgroud)bindingSource1.DataSource = LData; (LData is Generic List)
public DataRow currentRow
{
get
{
int position = this.BindingContext[bindingSource1].Position;
if (position > -1)
{
return ((DataRowView)bindingSource1.Current).Row;
}
else
{
return null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我无法获得当前行:
MessageBox.Show(currentRow["NAME"].ToString());
Run Code Online (Sandbox Code Playgroud)
出现错误:InvalidCastException,我该怎么办?谢谢。
是否可以取消在BindingSource.AddingNew事件处理程序中添加项目?
当用户单击 my 中的“添加新”按钮时BindingNavigator,我打开了一个对话框,该对话框可能会或可能不会返回有效文件。现在,我有这样的事情:
void bindingSource_AddingNew(object sender, AddingNewEventArgs e)
{
using (var dialog = new OpenFileDialog())
{
var result = dialog.ShowDialog();
// user canceled?
if (result == DialogResult.Cancel)
return;
// TryLoad will return null on failure
var data = TryLoad(dialog.FileName);
// only add the item to the grid if not null
if (data != null)
e.NewObject = data;
}
}
Run Code Online (Sandbox Code Playgroud)
即使我没有e.NewObject在处理程序中设置一个值,DataGridView 中也会出现一个新的(“空”)项。
是否可以取消添加项目?
当我尝试填充绑定源时,我收到一个错误.例外情况如下;
System.IndexOutOfRangeException: Index 0 does not have a value.
at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 rowIndex)
Run Code Online (Sandbox Code Playgroud)
我正在使用通用列表来填充绑定源.代码看起来像,
foreach (listItem)
{
BindingSource.Add(listItem);
}
Run Code Online (Sandbox Code Playgroud)
我尝试重置数据源属性,但仍然是同样的问题.
请帮我解决这个问题.
我似乎无法绑定到TextBox.这是我的来源:
public partial class formAirFreightLabels : Form
{
int pfDeclarationUID = -1;
BindingNavigator bindingNavigatorMain = new BindingNavigator();
BindingSource bindingSourceMain = new BindingSource();
public formAirFreightLabels(int pvDeclarationUID)
{
InitializeComponent();
pfDeclarationUID = pvDeclarationUID;
this.bindingNavigatorMain.BindingSource = this.bindingSourceMain;
this.bindingNavigatorMain.Dock = DockStyle.Bottom;
this.Controls.Add(this.bindingNavigatorMain);
this.Load += new EventHandler(formAirFreightLabels_Load);
}
void formAirFreightLabels_Load(object sender, EventArgs e)
{
SqlConnection cn = Program.GetSQLConnection();
if (cn != null)
{
SqlCommand cmd = new SqlCommand(String.Format("SELECT ShipperName, ShipperAddress, ConsigneeName, ConsigneeAddress FROM Declarations WHERE DeclarationUID={0}", pfDeclarationUID), cn);
SqlDataReader r = cmd.ExecuteReader(CommandBehavior.CloseConnection);
DataSet ds = new …Run Code Online (Sandbox Code Playgroud) 可能重复:
LINQ中有多个"order by"
我想知道如何通过两种不同的类型订购如下的查询:
var qry = (from m in dc.Orders
select new { m.OrderDate, m.OrderID }).OrderBy(o=>o.OrderDate+o.OrderID).ToList();
bindingSource1.DataSource = qry;
Run Code Online (Sandbox Code Playgroud)
OrdrerDate是日期类型,OrderID是int类型.
我正在尝试以编程方式向绑定源添加新行。我知道调用 bsSource.AddNew() 添加一个新行,我将其转换为 DataRowView 并设置我的值。我的问题是 - DataRowView.Row 显示 RowState 为 detached。我不希望它被分离;我相信它应该显示已添加 - 我也不希望它提交对数据库的更改(有一个非常有效的理由)。我想稍后选择时间。我的代码如下:
private Sub AddToRelationSource(binID As Integer, gradeID As Integer, IsChecked As Boolean)
Dim drv As DataRowView = DirectCast(bsBinGrades.AddNew(), DataRowView)
drv.Row("IsSelected") = IsChecked
drv.Row("BinID") = binID
drv.Row("GradeID") = gradeID
' I tried drv.EmdEdit(0 drv.Row.EndEdit() - Row State still shows detached
End Sub
Run Code Online (Sandbox Code Playgroud) 我需要修改一个DataTable连接到a DataGridView使用a BindingSource而不改变网格.
这是我的代码:
dtBallaFroc = new DataTable();
//...Fill the datatable
bindingSource.DataSource = dtBallaFroc;
gridView.DataSource = bindingSource;
Run Code Online (Sandbox Code Playgroud)
稍后在代码中我需要编辑数据表:
DataTable dataTable = (DataTable)bindingSource.DataSource;
for (int i = 0; i < dataTable.Rows.Count; i++)
{
dataTable.Rows[i][5] = 0;
}
Run Code Online (Sandbox Code Playgroud)
它可以正常工作,但它也编辑我的datagridview,我该如何阻止它?
bindingsource ×10
c# ×9
datagridview ×4
datasource ×2
winforms ×2
.net ×1
datarow ×1
datarowview ×1
dataset ×1
generic-list ×1
linq-to-sql ×1
sorting ×1
sql ×1
sql-order-by ×1
vb.net ×1