ASP.NET技巧:DataGridView常见问题
////AutoSizeColumnsMode指定如何设置列宽
//setGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
////AutoSizeRowsMode指定如何设置行高
//setGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
//// 设定列标题的宽度可以自由调整
//setGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
//// 设定行标题的宽度可以自由调整
//setGridView.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
//DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
//setGridView.Columns.Insert(0, column);
//列标题文本居中 DataGridViewColumn控件中的列
foreach (DataGridViewColumn col in setGridView.Columns)
{
col.SortMode = DataGridViewColumnSortMode.NotSortable;
}
setGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
//行高度 DataGridViewRow控件中的行
foreach (DataGridViewRow rows in setGridView.Rows)
{
rows.Height = 15;
}
//单击选取整行 SelectionMode指定如何设置单元格 DataGridViewSelectionMode可以如何选定单元格
setGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; //FullRowSelect通过单击表头和所包含的单元格选取整行
//设定不显示DateGridView的最左一列
setGridView.RowHeadersVisible = false;
2.
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["connectionstring"]);
string sql = " select top 10 * from news where type=1 order by sysdate desc";
SqlDataReader dr = DB.ExecuteSqlDR(sql);
this.GridView1.DataSource = dr;
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (((DropDownList)e.Row.FindControl("DDLXueli")) != null)
{
DropDownList ddlxueli = (DropDownList)e.Row.FindControl("DDLXueli");
// 生成 DropDownList 的值,绑定数据
string connStr = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
string SqlStr = "Select * from news";
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds, "zuozhe");
if (conn.State.ToString() == "Open") conn.Close();
ddlxueli.DataSource = ds.Tables[0].DefaultView;
ddlxueli.DataTextField = "author";
ddlxueli.DataValueField = "code";
ddlxueli.DataBind();
//
// 选中 DropDownList
ddlxueli.SelectedValue = ((HiddenField)e.Row.FindControl("HDFXueli")).Value;
//
}
}
protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
string id=GridView1.DataKeys[e.AffectedRows].ToString();
string conn=ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
try
{
}
catch
{
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
string card = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TBCard")).Text;
string xueli = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLXueli")).SelectedValue;
string price = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TBPrice")).Text;
string id1 = GridView1.DataKeys[e.RowIndex].Value.ToString();
string c=((TextBox)GridView1.Rows[e.RowIndex].FindControl("ds")).Text;
string id2=((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ds")).SelectedValue;
try
{
}
catch
{
}
}