how to refresh datagrid in windows mobile? by using c#.
开发者_运维百科how to clear to datagrid and re bind ?
datagrid bind to table. it is not refreshing when i insert another row
please give any solution for this.
i am writing for binding to 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;
I guess the question that needs to be asked is how are you adding the new row to the table? I have tried your code and everything looks good to me.
I have added new rows to the datatable on a button click and it refreshes the datagrid with no problems
Sample Project
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();
}
}
}
精彩评论