签到天数: 2089 天 连续签到: 1 天 [LV.Master]伴坛终老IIII
|
楼主 |
发表于 2009-3-17 16:31
|
显示全部楼层
GridView 动态添加列的方法及代码
GridView 动态添加列的方法及代码
代码如下: protected void Page_Load(object sender, EventArgs e)
{
TemplateField mycustomField = new TemplateField(); //创建列实例
mycustomField.ShowHeader = true; // 设置属性
LinkButton lb = new LinkButton();
lb.Text = \"Delete\";
mycustomField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, \"删除\");
mycustomField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, \"\", lb);
TabelContentListGridView.Columns.Add(mycustomField); //添加列到TabelContentListGridView控件
// 这种绑定翻页会丢失数据
// 下面的绑定数据可以保持 不过也要清零列不然后出现很多重复列
// TabelContentListGV.Columns.Clear(); //清零列
// System.Web.UI.WebControls.BoundField customField = new BoundField();
// customField.DataField = dt.Columns.ColumnName;
// customField.HeaderText = dt.Columns.ColumnName;
// customField.ItemStyle.CssClass = dt.Columns.ColumnName;
// TabelContentListGridView.Columns.Add(customField);
}
The first and second the way will run in the background thread, the third way will run in current thread.1. Use \"JTableReadTableModelTask \"to do this. (recommended)
Here is the sample code to import excel file:
JTable jTable = new JTable();
String excelFileName = \"excelFileName.xls\";
File file = new File(excelFileName ); //‘file’ is the file you want to load.
JProgressBarprogressBar = new JProgressBar(); //‘progressBar’ will show how much data it have loaded.
JTableReadTableModelTask task = new JTableReadTableModelTask(file, null, progressBar, jTable);
task.execute();
2.Use \"ReadTableModelTask\"to do this.
Use ReadTableModelTask, you must inherit it first, and override itsmethod done(),in the done method, you can use following statement toget a TableModel,this is not convenient, so we recommend the firstway to do this:
Object o = get();
if(o instanceof TableModel) {
TableModel model = (TableModel)get();
}
3.Use ModelIOto do this.
Here is the sample code, also you could put the following code into abackground thread, For example: use javax.swing.SwingWorker.
Map m = new HashMap();
// you could set progressBar in the map,for example: m.put(ModelIO.PROGRESS_BAR, progressBar);
WorkBook book = ModelIO.readWorkBook(openUrl, format, m);
TableModel tableModel;
if(book.getSelectedSheet() != null) {
tableModel = book.getSelectedSheet().getModel();
} else if(book.getSheetCount() > 0) {
tableModel = book.getSheet(0).getModel();
} |
|