如何刷新windows mobile中的datagrid?使用c#。
如何清除datagrid并重新绑定?
datagrid绑定到表。当我插入另一行时,它并不令人耳目一新
请为此提供任何解决方案。
我写的是绑定到datagrid
DataTable dataTable = dsRates.Tables [0];
DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = dataTable.TableName;
GridColumnStylesCollection columnStyles = tableStyle.GridColumnStyles;
DataGridTextBoxColumn columnStyle = new DataGridTextBoxColumn();
columnStyle.MappingName = "MortgageTypeName";
columnStyle.HeaderText = "Years";
columnStyle.Width = 58;
columnStyles.Add(columnStyle);
columnStyle = new DataGridTextBoxColumn();
columnStyle.MappingName = "FixedRate";
columnStyle.HeaderText = "Fixed(%)";
columnStyle.Width = 64;
columnStyles.Add(columnStyle);
columnStyle = new DataGridTextBoxColumn();
columnStyle.MappingName = "ARMRate";
columnStyle.HeaderText = "ARM(%)";
columnStyle.Width = 64;
columnStyles.Add(columnStyle);
GridTableStylesCollection tableStyles = dgRates.TableStyles;
tableStyles.Add(tableStyle);
dgRates.PreferredRowHeight = 16;
dgRates.RowHeadersVisible = false;
dgRates.DataSource = dataTable;
答案 0 :(得分:1)
我想需要问的问题是你如何将新行添加到表中?我已经尝试过你的代码了,一切对我来说都很好。
我在按钮单击时向数据表添加了新行,并且没有任何问题刷新数据网格
namespace SmartDeviceProject1
{
public partial class Form1 : Form
{
DataTable dataTable = new DataTable();
public Form1()
{
InitializeComponent();
dataTable.Columns.Add("MortgageTypeName", typeof(String));
dataTable.Columns.Add("FixedRate", typeof(Int32));
dataTable.Columns.Add("ARMRate", typeof(Int32));
DataRow dr = dataTable.NewRow();
dr["MortgageTypeName"] = "Fixed";
dr["FixedRate"] = 5;
dr["ARMRate"] = 10;
dataTable.Rows.Add(dr);
DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = dataTable.TableName;
GridColumnStylesCollection columnStyles = tableStyle.GridColumnStyles;
DataGridTextBoxColumn columnStyle = new DataGridTextBoxColumn();
columnStyle.MappingName = "MortgageTypeName";
columnStyle.HeaderText = "Years";
columnStyle.Width = 58;
columnStyles.Add(columnStyle);
columnStyle = new DataGridTextBoxColumn();
columnStyle.MappingName = "FixedRate";
columnStyle.HeaderText = "Fixed(%)";
columnStyle.Width = 64;
columnStyles.Add(columnStyle);
columnStyle = new DataGridTextBoxColumn();
columnStyle.MappingName = "ARMRate";
columnStyle.HeaderText = "ARM(%)";
columnStyle.Width = 64;
columnStyles.Add(columnStyle);
GridTableStylesCollection tableStyles = dgRates.TableStyles;
tableStyles.Add(tableStyle); dgRates.PreferredRowHeight = 16;
dgRates.RowHeadersVisible = false;
dgRates.DataSource = dataTable;
}
private void btnAdd_Click(object sender, EventArgs e)
{
DataRow dr = dataTable.NewRow();
dr["MortgageTypeName"] = "Fixed";
dr["FixedRate"] = 6;
dr["ARMRate"] = 11;
dataTable.Rows.Add(dr);
}
private void btnClear_Click(object sender, EventArgs e)
{
dataTable.Rows.Clear();
}
}
}