将数据绑定到“Word 操作”窗格上的控件

image

向项目中添加新的数据源

1.创建一个名为“我的 Word 操作窗格”的Word 文档项目。 在向导中,选择“创建新文档”。

2.在“解决方案资源管理器”中选择“我的 Word 操作窗格”项目。

3.在“项目”菜单上,单击“添加新项”。

4.在“添加新项”对话框中选择“操作窗格控件”,将它命名为“ActionsControl”,然后单击“添加”

5.如果“数据源”窗口不可见,请单击“数据”菜单上的“显示数据源”。

6.单击“添加新数据源”以启动“数据源配置向导”。

7.选择到 Northwind 示例 SQL Server 数据库的数据连接,或者使用“新建连接”按钮添加新连接。

8.展开“数据库对象”窗口中的“表”节点。

9.选择“Suppliers”表和“Products”表旁边的复选框。

10.单击“完成”。

将数据绑定 Windows 窗体控件添加到操作窗格控件

1.在“数据源”窗口中展开“Suppliers”表。单击“Company Name”节点上的下拉箭头并选择“ComboBox”。

~@056F9$XT]558KK)J0I[0E

2.将“CompanyName”从“数据源”窗口拖动到操作窗格控件中。

操作窗格控件中便会创建一个 ComboBox 控件。 同时,会将一个名为 SuppliersBindingSource 的 BindingSource、一个表适配器和一个 DataSet 添加到组件栏的项目中。

3.选择“组件”栏中的 SuppliersBindingNavigator 并按 Delete。

4.在“数据源”窗口中,展开“Suppliers”表的子表“Products”。

5.单击“ProductName”节点上的下拉箭头,然后选择“ListBox”。

6.将“ProductName”拖动到操作窗格控件中。

7.右击 Button,单击快捷菜单上的“属性”并更改以下属性

Name:insert,文本:插入

使用数据加载控件

1.在 ActionsControl 类的 Load 事件处理程序中,添加以下代码。

private void ActionsControl_Load(object sender, EventArgs e)
{
this.suppliersTableAdapter.Fill(this.northwindDataSet.Suppliers);
this.productsTableAdapter.Fill(this.northwindDataSet.Products);
}

2.在 C# 中,必须将事件处理程序附加到 Load 事件。 可以将这些代码放在 ActionsControl 构造函数中 InitializeComponent调用的后面。

this.Load += new EventHandler(ActionsControl_Load);

设置控件的数据绑定属性
  1. 选择 CompanyNameComboBox 控件。

  2. 在“属性”窗口中单击“DataSource”属性右侧的按钮,然后选择“SuppliersBindingSource”。

  3. 单击“DisplayMember”属性右侧的按钮,然后选择“CompanyName”。

  4. 展开“DataBindings”属性,单击“Text”属性右侧的按钮,然后选择“None”。

  5. 选择 ProductNameListBox 控件。

  6. 在“属性”窗口中单击“DataSource”属性右侧的按钮,然后选择“productsBindingSource”。

  7. 单击“DisplayMember”属性右侧的按钮,然后选择“ProductName”。

  8. 展开“DataBindings”属性,单击“SelectedValue”属性右侧的按钮,然后选择“None”。

 

添加方法以将数据插入表中

下一个任务是从绑定控件中读取数据并填充 Word 文档中的表。 首先,创建一个用于设置表格标题格式的过程,然后添加 AddData 方法以创建一个 Word 表并设置其格式。

设置表格标题的格式

在 ActionsControl 类中创建一个方法,用以设置表格标题的格式。

static void SetHeadings(Microsoft.Office.Interop.Word.Cell tblCell, string text)
{
tblCell.Range.Text = text;
tblCell.Range.Font.Bold = 1;
tblCell.Range.ParagraphFormat.Alignment =
Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
}

创建表

在 ActionsControl 类中编写一个方法,此方法将在不存在表的情况下创建一个表,并将操作窗格中的数据添加到此表中。

private void AddData(System.Data.DataRow row, string companyName)
{
object missing = System.Type.Missing;

// Create a table if it doesn't already exist.
if (Globals.ThisDocument.Tables.Count == 0)
{
try
{
// Create a table.
Microsoft.Office.Interop.Word.Table tbl = Globals.ThisDocument.Tables.Add
(Globals.ThisDocument.Application.Selection.Range, 1, 4, ref missing, ref missing);

// Insert headings.
SetHeadings(tbl.Cell(1, 1), "Company Name");
SetHeadings(tbl.Cell(1, 2), "Product Name");
SetHeadings(tbl.Cell(1, 3), "Quantity");
SetHeadings(tbl.Cell(1, 4), "Unit Price");
}
catch (Exception ex)
{
MessageBox.Show("Problem creating Products table: " + ex.Message,
"Actions Pane", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

// Add data from data row to the table.
Microsoft.Office.Interop.Word.Selection selection = Globals.ThisDocument.Application.Selection;

if (selection.Tables.Count > 0)
{
Microsoft.Office.Interop.Word.Row newRow = Globals.ThisDocument.Tables[1].Rows.Add(ref missing);

newRow.Range.Font.Bold = 0;

newRow.Range.ParagraphFormat.Alignment =
Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;

newRow.Cells[4].Range.ParagraphFormat.Alignment =
Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;

newRow.Cells[1].Range.Text = companyName;
newRow.Cells[2].Range.Text = row["ProductName"].ToString();
newRow.Cells[3].Range.Text = row["QuantityPerUnit"].ToString();
newRow.Cells[4].Range.Text = Math.Round(Convert.ToDouble(row["UnitPrice"])).ToString("#,##0.00");
}
else
{
MessageBox.Show("Cursor must be within a table.",
"Actions Pane", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

在 Word 表中插入文本

1.将以下代码添加到“插入”按钮的 Click 事件处理程序中。

private void Insert_Click(object sender, System.EventArgs e)
{
System.Data.DataTable tbl = northwindDataSet.Products;
System.Data.DataRow[] rows;

// Check if a product is selected.
if (this.productNameListBox.SelectedIndex >= 0)
{
System.Data.DataRowView productRow = (System.Data.DataRowView)this.productNameListBox.SelectedItem;

string product = productRow.Row["ProductName"].ToString();
string company = this.companyNameComboBox.Text;

// Return the data row from the selected product.
rows = tbl.Select("[ProductName] = '" + product.Replace("'", "''") + "'");

this.AddData(rows[0], company);
}
else
{
MessageBox.Show("Please select a product.", "Actions Pane", MessageBoxButtons.OK);
}
}

2.在 C# 中,必须为按钮的 Click 事件创建事件处理程序。 可以将此代码放置在 ActionsControl 类的 Load 事件处理程序中。

this.Insert.Click += new EventHandler(Insert_Click);

显示操作窗格

1.在“解决方案资源管理器”中右击“ThisDocument.vb”或“ThisDocument.cs”,再单击快捷菜单上的“查看代码”。

2.在 ThisDocument 类的顶部创建一个新的控件实例,使其看上去与下面的示例类似。

private ActionsControl actions = new ActionsControl();

3.向 ThisDocument 的 Startup 事件处理程序中添加代码,使其看上去与下面的示例类似。

this.ActionsPane.Controls.Add(actions);

测试应用程序

现在可以对文档进行测试,以验证当文档打开时是否出现操作窗格。 测试操作窗格上控件之间的主/从关系,并确保单击“插入”按钮时数据会填充到 Word 表中

  1. 按 F5 运行项目。

  2. 确认操作窗格可见。

  3. 从组合框中选择一个公司,并确认“Products”列表框中的项随之发生变化。

  4. 选择一个产品,单击操作窗格上的“插入”,并确认产品详细信息会随之添加到 Word 表中。

  5. 插入来自不同公司的其他产品。

源码下载:/Files/timy/MyBasicActionsPane.rar

原文地址:https://www.cnblogs.com/timy/p/2007203.html