PDA

View Full Version : عدم نمایش دیتا گرید



faramarz_s
شنبه 29 شهریور 1382, 11:49 صبح
با کد زیر پیغام خطایی نمی دهد اما اصلا دیتاگ ریدی نمایش نمی دهد.

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
SqlConnection conn = new
SqlConnection("server=(local);uid=sa;pwd=yaali;database=MDB" );
//if(!Page.IsPostBack){
// BindGrid();
SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * FROM tblPrimary", conn);

DataSet ds = new DataSet();
myCommand.Fill(ds,"tblPrimary");
DataView dv= ds.Tables["tblPrimary"].DefaultView;
dv.Sort="id";

MyDataGrid.DataSource=dv;
MyDataGrid.DataBind();

}

Vahid_Nasiri
شنبه 29 شهریور 1382, 14:45 عصر
DataView را فراموش نکرده ای؟

faramarz_s
شنبه 29 شهریور 1382, 21:29 عصر
در فصل 8 جزوه شما برای نمایش داده در دیتا گرید از دیتا ویو استفاده نکرده اید ،فقط در مثالی که سورت را انجام می دهد دیتا ویو هست اصلا این کد مشکلی دارد؟

public void BindGrid()
{
SqlConnection conn = new SqlConnection(
"server=(local);uid=sa;pwd=yaali;database=MDB");
SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * FROM tblPrimary", conn);

DataSet ds = new DataSet();
myCommand.Fill(ds);
MyDataGrid.DataSource=ds.Tables["tblPrimary"].DefaultView;
MyDataGrid.DataBind();
}

faramarz_s
شنبه 29 شهریور 1382, 22:06 عصر
جناب نصیری این مثال خود مایکروسافته!

<script language="C#" runat="server">
SqlConnection myConnection;

protected void Page_Load(Object Src, EventArgs E)
{
// Create a connection to the "pubs" SQL database located on the
// local computer.
myConnection = new SqlConnection ("server=localhost;uid=sa;pwd=yaali;" +
"database=pubs;Trusted_Connection=Yes");
// Determine whether the page is a postback. If it is not a
// postback, call BindGrid.
if (!IsPostBack) BindGrid();
}
// Create an index to the DataGrid row that is clicked and
// call BindGrid.
public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs E)
{
MyDataGrid.EditItemIndex = (int)E.Item.ItemIndex;
BindGrid();
}

// Cancel resets the index to the row's previous settings.
public void MyDataGrid_Cancel(Object sender,
DataGridCommandEventArgs E)
{
MyDataGrid.EditItemIndex = -1;
BindGrid();
}
// When the Update link is clicked, build a SQL UPDATE command,
// connect to the database, update the row's information in the
// database, and rebind the DataGrid to show the updated information.
public void MyDataGrid_Update(Object sender,
DataGridCommandEventArgs E)
{
String updateCmd = "UPDATE Authors SET au_id = @Id," +
" au_lname = @LName, au_fname = @FName, phone = @Phone," +
" address = @Address, city = @City, state = @State," +
" zip = @Zip, contract = @Contract WHERE au_id = @Id;";
SqlCommand myCommand = new SqlCommand(updateCmd, myConnection);
myCommand.Parameters.Add(new SqlParameter("@Id",
SqlDbType.VarChar));
myCommand.Parameters.Add(new SqlParameter("@LName",
SqlDbType.VarChar));
myCommand.Parameters.Add(new SqlParameter("@FName",
SqlDbType.VarChar));
myCommand.Parameters.Add(new SqlParameter("@Phone",
SqlDbType.VarChar));
myCommand.Parameters.Add(new SqlParameter("@Address",
SqlDbType.VarChar));
myCommand.Parameters.Add(new SqlParameter("@City",
SqlDbType.VarChar));
myCommand.Parameters.Add(new SqlParameter("@State",
SqlDbType.VarChar));
myCommand.Parameters.Add(new SqlParameter("@Zip",
SqlDbType.VarChar));
myCommand.Parameters.Add(new SqlParameter("@Contract",
SqlDbType.VarChar));
// Initialize the SqlCommand "@Id" parameter to the ID of the
// row that must be clicked.
myCommand.Parameters["@Id"].Value =
MyDataGrid.DataKeys[(int)E.Item.ItemIndex];
// Create an array of column names.
String[] cols = {"@Id","@LName","@FName","@Phone",
"@Address", "@City","@State","@Zip","@Contract"};
// Iterate through the columns, checking for empty values.
// Skip the first, second, and last columns. If an empty value
// is found, display an error message box. Also initialize the
// SqlCommand parameter values.
int numCols = E.Item.Cells.Count;
for (int i=2; i<numCols-1; i++)
{
String colvalue = ((TextBox)E.Item.Cells[i].Controls[0]).Text;
if (i<6 && colvalue == "")
{
Message.InnerHtml = "ERROR: Null values not allowed for" +
" Author ID, Name or Phone";
Message.Style["color"] = "red";
return;
}
myCommand.Parameters[cols[i-1]].Value = colvalue;
}
// Append the last field, converting true/false values to 0/1.
if (String.Compare(((TextBox)E.Item.Cells
[numCols-1].Controls[0]).Text, "true", true)==0)
myCommand.Parameters["@Contract"].Value = "1";
else
myCommand.Parameters["@Contract"].Value = "0";
// Connect to the database and update the information.
myCommand.Connection.Open();
// Test whether the data was updated and display the
//appropriate message to the user.
try
{
myCommand.ExecuteNonQuery();
Message.InnerHtml = "<b>Record Updated.</b><br>";
MyDataGrid.EditItemIndex = -1;
}
catch (SqlException e)
{
if (e.Number == 2627)
Message.InnerHtml = "ERROR: A record already exists" +
" with the same primary key";
else
Message.InnerHtml = "ERROR: Could not update record," +
" please ensure the fields are correctly filled out";
Message.Style["color"] = "red";
}
// Close the connection.
myCommand.Connection.Close();
// Show the updated information.
BindGrid();
}

// The BindGrid procedure connects to the database and implements
// a SQL SELECT query to get all data in the "Authors" table.
public void BindGrid()
{
SqlConnection myConnection = new SqlConnection(
"server=localhost;uid=sa;pwd=yaali;database=pubs;Tr usted_Connection=Yes");
SqlDataAdapter myCommand = new SqlDataAdapter(
"SELECT * FROM authors", myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds);
MyDataGrid.DataSource=ds;
MyDataGrid.DataBind();

من هم کپی کرده و تغییراتی بسیار جزیی دادم اما تنها صفحه ای خالی بدون پیغام خطا را نمایش میدهد!

Vahid_Nasiri
یک شنبه 30 شهریور 1382, 01:09 صبح
یک سری آدرس مقاله در مورد دیتاگرید اینجا هست:
http://www.barnamenevis.org/forum/viewtopic.php?t=2346
فکر کنم برای 6 ماه کافی باشد. :wink:

faramarz_s
یک شنبه 30 شهریور 1382, 10:04 صبح
جناب نصیری خودمونیما حوصله که نداشته باشین به نخود سیاه حواله می دهید :lol:
باشه!