1)简单一些,我把gridview里面倒入到excel后连里面的副选框,但选按钮,都进去了。如何不让那些控件 倒入阿,只倒入数据。
2) gridview 单击或者双击表格内容可编辑,鼠标点到其他的位置后,保存
3)一个表5万条数据,查询的时候有where条件,如何加快访问绑定数据的速度呢?1和2 需要源代码。
2) gridview 单击或者双击表格内容可编辑,鼠标点到其他的位置后,保存
3)一个表5万条数据,查询的时候有where条件,如何加快访问绑定数据的速度呢?1和2 需要源代码。
/// 把DataTable里的内容导出到EXCEL,执行代码就行,什么都不用改,什么都不用设置
/// </summary>
/// <param name="tb"></param>
public static void TableToExcel(System.Data.DataTable tb)
{
System.Web.UI.WebControls.DataGrid dgrid = null;
System.Web.HttpContext context = System.Web.HttpContext.Current;
System.IO.StringWriter strOur = null;
System.Web.UI.HtmlTextWriter htmlWriter = null; if (tb != null)
{
context.Response.ContentType = "application/vnd.ms-excel ";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.Charset = "";
//独立打开excel软件
context.Response.AppendHeader("Content-disposition", "attachment; filename=OutData.xls");
strOur = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strOur);
dgrid = new System.Web.UI.WebControls.DataGrid();
dgrid.DataSource = tb.DefaultView;
dgrid.AllowPaging = false;
dgrid.DataBind();
dgrid.RenderControl(htmlWriter);
context.Response.Write(strOur.ToString());
context.Response.End();
// '嵌套在ie里打开excel
// ' context.Response.AppendHeader("Content-disposition","inline; filename="+filename+".xls");
// ' Response.Charset = "GB2312"; 中文名
// 'Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
}
}
System.Web.UI.WebControls.GridView dgExport = null;
// IO用于导出并返回excel文件
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null; if (mytable != null)//绑定GridView的数据集
{
// 设置编码和附件格式
Response.ContentType = "application nd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment;filename=Borrow.xls");
Response.Charset = "";
Response.ContentEncoding = System.Text.Encoding.UTF8; // 导出excel文件
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter); // 为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的DataGrid
dgExport = new System.Web.UI.WebControls.DataGrid();
dgExport.DataSource = mytable.DefaultView;
dgExport.AllowPaging = false;
dgExport.DataBind(); // 返回客户端
dgExport.RenderControl(htmlWriter);
Response.Write(strWriter.ToString());
Response.End();
}
#region 导出 Excel 文件
/// <summary>
/// 导出 Excel 文件
/// </summary>
/// <param name="ds">要导出的DataSet</param>
/// <param name="strExcelFileName">要导出的文件名</param>
public static void ExportExcel(DataSet ds, string strExcelFileName)
{
object objOpt = Missing.Value;
Application excel = new Application();
excel.Visible = true;
_Workbook wkb = excel.Workbooks.Add(objOpt);
_Worksheet wks = (_Worksheet)wkb.ActiveSheet; wks.Visible = XlSheetVisibility.xlSheetVisible; int rowIndex = 1;
int colIndex = 0; System.Data.DataTable table = ds.Tables[0];
foreach (DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[1, colIndex] = col.ColumnName;
} foreach (DataRow row in table.Rows)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
}
}
//excel.Sheets[0] = "sss";
wkb.SaveAs(strExcelFileName, objOpt, null, null, false, false, XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
wkb.Close(false, objOpt, objOpt);
excel.Quit();
}
#endregion
第二个:给gridview行加单击事件 网上有源码找
第三个:建索引,用rownumber() (2005数据库)
这个没有人会吗?
第3个问题,你可以打开MSSQL的事件查看器,自己跟踪下,就知道了~
// 声明一个Customer对象来保存当前编辑对象的数据
private Customer customerInEdit;
// 声明一个变量来存储当前编辑的行索引
// 如果值为-1,表示没有任何行处于编辑状态
private int rowInEdit = -1;
// 声明一个变量来表示提交的范围,如果为true,表示提交行,否则提交一个单元格
private bool rowScopeCommit = true;
{
// 使用虚拟模式
this.dataGridView1.VirtualMode = true; // 为虚拟模式相关的事件添加处理函数
this.dataGridView1.CellValueNeeded += new
DataGridViewCellValueEventHandler(dataGridView1_CellValueNeeded);
this.dataGridView1.CellValuePushed += new
DataGridViewCellValueEventHandler(dataGridView1_CellValuePushed);
this.dataGridView1.NewRowNeeded += new
DataGridViewRowEventHandler(dataGridView1_NewRowNeeded);
this.dataGridView1.RowValidated += new
DataGridViewCellEventHandler(dataGridView1_RowValidated);
this.dataGridView1.RowDirtyStateNeeded += new
QuestionEventHandler(dataGridView1_RowDirtyStateNeeded);
this.dataGridView1.CancelRowEdit += new
QuestionEventHandler(dataGridView1_CancelRowEdit);
this.dataGridView1.UserDeletingRow += new
DataGridViewRowCancelEventHandler(dataGridView1_UserDeletingRow); // 为DataGridView添加列
DataGridViewTextBoxColumn companyNameColumn = new DataGridViewTextBoxColumn();
companyNameColumn.HeaderText = "Company Name";
companyNameColumn.Name = "Company Name";
DataGridViewTextBoxColumn contactNameColumn = new DataGridViewTextBoxColumn();
contactNameColumn.HeaderText = "Contact Name";
contactNameColumn.Name = "Contact Name";
this.dataGridView1.Columns.Add(companyNameColumn);
this.dataGridView1.Columns.Add(contactNameColumn);
this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; // 添加示例数据
this.customers.Add(new Customer("Bon app'", "Laurence Lebihan"));
this.customers.Add(new Customer("Bottom-Dollar Markets", "Elizabeth Lincoln"));
this.customers.Add(new Customer("B's Beverages", "Victoria Ashworth")); // 设置行数,包括新行
this.dataGridView1.RowCount = 4;
}
private void dataGridView1_CellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
{
// 如果为新行,不需要任何值
if (e.RowIndex == this.dataGridView1.RowCount - 1) { return; } Customer customerTmp = null; // 为要绘制的行保存Customer对象的引用;
if (e.RowIndex == rowInEdit)
{
customerTmp = this.customerInEdit;
}
else
{
customerTmp = (Customer)this.customers[e.RowIndex];
} // 用获得的Customer对象来绘制单元格的值
switch (this.dataGridView1.Columns[e.ColumnIndex].Name)
{
case "Company Name":
e.Value = customerTmp.CompanyName;
break; case "Contact Name":
e.Value = customerTmp.ContactName;
break;
}
}
private void dataGridView1_CellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
{
Customer customerTmp = null; // 保存当前编辑行对应的Customer对象的引用;
if (e.RowIndex < this.customers.Count)
{
// 如果用户在编辑新行,创建一个新的Customer对象;
if (this.customerInEdit == null)
{
this.customerInEdit = new Customer(
((Customer)this.customers[e.RowIndex]).CompanyName,
((Customer)this.customers[e.RowIndex]).ContactName);
}
customerTmp = this.customerInEdit;
this.rowInEdit = e.RowIndex;
}
else
{
customerTmp = this.customerInEdit;
} // 保存单元格的值到Customer对象的属性;
String newValue = e.Value as String;
switch (this.dataGridView1.Columns[e.ColumnIndex].Name)
{
case "Company Name":
customerTmp.CompanyName = newValue;
break; case "Contact Name":
customerTmp.ContactName = newValue;
break;
}
}
private void dataGridView1_NewRowNeeded(object sender, System.Windows.Forms.DataGridViewRowEventArgs e)
{
// 如果要编辑新行,创建一个新的Customer对象;
this.customerInEdit = new Customer();
this.rowInEdit = this.dataGridView1.Rows.Count - 1;
}
// 该事件发生在 对一行的验证完成后
private void dataGridView1_RowValidated(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
{
// 保存所做的任何修改,释放当前编辑对象的引用
// 第一种情况是 该行为“旧”的新行
if (e.RowIndex >= this.customers.Count && e.RowIndex != this.dataGridView1.Rows.Count - 1)
{
// 添加新对象到数据源
this.customers.Add(this.customerInEdit);
this.customerInEdit = null;
this.rowInEdit = -1;
}
// 第二种情况是 该行为旧行
else if (this.customerInEdit != null && e.RowIndex < this.customers.Count)
{
// 保存修改后的数据源
this.customers[e.RowIndex] = this.customerInEdit;
this.customerInEdit = null;
this.rowInEdit = -1;
}
// 第二种情况是 该行为新行,此时放弃修改
else if (this.dataGridView1.ContainsFocus)
{
this.customerInEdit = null;
this.rowInEdit = -1;
}
}
// 该事件用于查看 是否有未提交的修改
private void dataGridView1_RowDirtyStateNeeded(object sender, System.Windows.Forms.QuestionEventArgs e)
{
if (!rowScopeCommit)
{
// In cell-level commit scope, indicate whether the value
// of the current cell has been modified.
e.Response = this.dataGridView1.IsCurrentCellDirty;
}
}
// 如果用户 取消了对一行的编辑
private void dataGridView1_CancelRowEdit(object sender, System.Windows.Forms.QuestionEventArgs e)
{
if (this.rowInEdit == this.dataGridView1.Rows.Count - 2 && this.rowInEdit == this.customers.Count)
{
// 如果用户取消了对新添加行的编辑,则使用一个新的空对象表示当前编辑对象;
this.customerInEdit = new Customer();
}
else
{
// 如果取消了对现有行的编辑,释放相应的对象
this.customerInEdit = null;
this.rowInEdit = -1;
}
}
// 如果用户 要删除一行
private void dataGridView1_UserDeletingRow(object sender, System.Windows.Forms.DataGridViewRowCancelEventArgs e)
{
if (e.Row.Index < this.customers.Count)
{
// If the user has deleted an existing row, remove the
// corresponding Customer object from the data store.
this.customers.RemoveAt(e.Row.Index);
} if (e.Row.Index == this.rowInEdit)
{
// If the user has deleted a newly created row, release
// the corresponding Customer object.
this.rowInEdit = -1;
this.customerInEdit = null;
}
}
<table border="0" cellpadding="0" cellspacing="1" class="list" onclick="clickevent();"》
<input type="hidden" name="rowi" id="rowi">
function clickevent(){
var e=event.srcElement;
if(e.tagName!="TD")return;
e=GetParentElement(e, "TR");
if(e.rowIndex<=0)return;
var ri=document.getElementById("rowi");
ri.value=e.rowIndex;保存用AJAXfunction save(){
var obj=document.getElementById("table1");
var dt = new Ajax.Web.DataTable();
dt.addColumn("id", "System.Int32");
dt.addColumn("setname", "System.String");
dt.addColumn("setvalue", "System.String");
for(var i=1;i<obj.rows.length;i++){
//dt.addRow({"setname":"aaaaa","setvalue":"bbbbb"});
var row = new Object();
row.id = obj.rows(i).cells(0).innerText
row.setname = obj.rows(i).cells(1).innerText;
row.setvalue = obj.rows(i).cells(2).innerText;
dt.addRow(row);
}
//alert(dt.Rows[0][dt.Columns[0].Name]);
var result = Setting.updateData(dt).value;
if(result>0)
alert("系统设置更新成功!");
else
alert("系统设置更新失败!");
}
}
}