I have DetailsView with connected SqlDataSource and I want cancel updating row when my condition is true in OnRowUpdating event
I did this step but when condition is true DetailsView still at editing mode all I need to return to normal view if the condition is true
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
// Iterates through the rows of the GridView control
foreach (DetailsViewRow row in DetailsView1.Rows)
{
// Selects the text from the TextBox
// which is inside the GridView control
// Selects the text from the DropDownList
// which is inside the GridView control
string dropDownListText = ((DropDownList)
row.FindControl("DropDownList12")).SelectedItem.Text;
SqlConnection conn = new SqlConnection(GetConnectionString());
SqlCommand cmd2 = new SqlCommand();
conn.Open();
cmd2.CommandText = #"Select StatusID from ComplainMain Where TicketID=#tktid";
cmd2.Parameters.AddWithValue("#tktid", ticketid.tktid);
cmd2.Connection = conn;
SqlDataReader rdr = cmd2.ExecuteReader();
int status;
while (rdr.Read())
{
status = rdr.GetInt32(0);
if (status == 3)
{
Label18.Visible = true;
Label18.Text = "Can not Modify this Complaint as Ticket Closed refer to the Admin";
e.Cancel = true;
}
}
}
}
Page_Load
protected void Page_Load(object sender, EventArgs e)
{
Label18.Visible = false;
DetailsView1.ItemUpdating += new DetailsViewUpdateEventHandler(DetailsView1_ItemUpdating);
}
Use ChangeMode method on your DetailsView
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
Related
i am newbie to dev express.
i want to change the row background color in dev express grid after the data is loaded in C# winforms.
i am populating the data grid from the below code
string sReportSql = "SELECT * FROM Employee";
private void Form_Load(object sender, EventArgs e)
{
dataGridView.DataSource = GeDataFromDb();
}
private DataTable GeDataFromDb()
{
DataTable dtGenericReport = new DataTable();
using (SqlConnection con = new SqlConnection(connString))
{
if (sReportSql != null)
{
using (SqlCommand cmd = new SqlCommand(sReportSql, con))
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
dtGenericReport.Load(reader);
}
}
}
return dtGenericReport;
}
i tried using the Row Style event but it does not seems to be working
private void gridview1_RowStyle(object sender, RowStyleEventArgs e)
{
GridView View = sender as GridView;
if (e.RowHandle >= 0) {
string status = View.GetRowCellDisplayText(e.RowHandle, View.Columns["status"]);
if (status == "Completed") {
e.Appearance.BackColor = Color.IndianRed;
}
}
}
The best way that I know is to use CustomDrawCell event. Something like the following code.
private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
GridView View = sender as GridView;
if (e.RowHandle >= 0) {
string status = View.GetRowCellDisplayText(e.RowHandle, View.Columns["status"]);
if (status == "Completed") {
e.Appearance.BackColor = Color.IndianRed;
}
}
}
It seems to me that there is no problem in your code
I think the name of event gridview1_RowStyle should be gridview_RowStyle
you may have made a mistake by selecting another gridview
you can check it in Run designer
I have 3 gridviews in 3 different updatepanels, which i update them after RowCommand.
GridView1.DataBind();
GridView2.DataBind();
GridView3.DataBind();
The weird thing is only GridView1_RowCommand can update all three gridview. GridView2_RowCommand using the same method but i cannot update the gridview.
Please help me.
Thanks in advance.
Here is the code for Rowcommand:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//check the commandName
if (e.CommandName != "SaveStartTime")
return;
int rowIndex = int.Parse(e.CommandArgument.ToString());
string id = GridView1.Rows[rowIndex].Cells[0].Text;
Label time = GridView1.Rows[rowIndex].Cells[4].FindControl("ActualTimeStart") as Label;
time.Text = DateTime.Now.ToString("HH:mm");
var Cn = new System.Data.SqlClient.SqlConnection();
Cn.ConnectionString = "Server=.\\SqlExpress;Database=CMOS;Trusted_Connection=True";
Cn.Open();
var Cm = Cn.CreateCommand();
string store = string.Format(#"UPDATE [ApprovedExitPass] SET ActualTimeStart = '{0}' WHERE Id='{1}'", time.Text, id);
SqlCommand cmd = new SqlCommand(store, Cn);
cmd.Parameters.AddWithValue("#ActualTimeStart", time.Text);
cmd.ExecuteNonQuery();
Cn.Close();
GridView1.DataBind();
GridView2.DataBind();
GridView3.DataBind();
}
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
//check the commandName
if (e.CommandName != "SaveReturnTime")
return;
int rowIndex = int.Parse(e.CommandArgument.ToString());
string id = GridView2.Rows[rowIndex].Cells[0].Text;
Response.Write(id);
Label time = GridView2.Rows[rowIndex].Cells[4].FindControl("ActualTimeArrive") as Label;
time.Text = DateTime.Now.ToString("HH:mm");
var Cn = new System.Data.SqlClient.SqlConnection();
Cn.ConnectionString = "Server=.\\SqlExpress;Database=CMOS;Trusted_Connection=True";
Cn.Open();
var Cm = Cn.CreateCommand();
string store = string.Format(#"UPDATE [ApprovedExitPass] SET ActualTimeArrive = '{0}' WHERE Id='{1}'", time.Text, id);
SqlCommand cmd = new SqlCommand(store, Cn);
cmd.Parameters.AddWithValue("#ActualTimeArrive", time.Text);
cmd.ExecuteNonQuery();
Cn.Close();
GridView1.DataBind();
GridView2.DataBind();
GridView3.DataBind();
}
YES,gridview1_rowcommand will update all three because if you see the gridview1_rowcommand.in that method you are calling the databind of other two gridviews also . so now why this happening only while calling gridview1_rowcommand because you might be probably calling first grid row command or some other Issue.
SO probably Verify whether you getting value in Response.write(id)
i have a data grid view combo box column in my data grid view. On on load event of form i am loading the companies name in combo box column and through cell formatting event i am giving the default value to data grid view combo box column.
Now if i select different value from combo box column it does not get reflected. means only default value is there.
My code for on load and cell formatting is
public void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
SqlConnection c = new SqlConnection();
c.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename='D:\\Documents\\Visual Studio 2008\\Projects\\Accounts\\Accounts\\Database1.mdf';Integrated Security=True;User Instance=True";
c.Open();
if (e.ColumnIndex == 1 && Viewcashvoucher.mk != "")
{
string mky = Viewcashvoucher.mk;
string q = "select * from lgr where main_key ='" + mky + "'";
SqlCommand cmd = new SqlCommand(q, c);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
e.Value = dr["account_n"].ToString();
}
}
c.Close();
}
and on load event is
private void Cash_Voucher_Load(object sender, EventArgs e)
{
string mky = Viewcashvoucher.mk;
SqlConnection c = new SqlConnection();
c.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename='D:\\Documents\\Visual Studio 2008\\Projects\\Accounts\\Accounts\\Database1.mdf';Integrated Security=True;User Instance=True";
c.Open();
string q = "select max(date) from lgr ";
SqlCommand cmd = new SqlCommand(q, c);
SqlDataReader rd = cmd.ExecuteReader();
try
{
if (rd.Read())
{
DateTime date = rd.GetDateTime(0);
if (date != null)
{
tbdate.Text = date.ToShortDateString();
}
}
}
catch (Exception ex)
{
tbdate.Text = DateTime.Now.ToShortDateString();
}
c.Close();
c.Open();
string q1 = "select account_n from master";
SqlCommand cmd1 = new SqlCommand(q1, c);
SqlDataReader rd1 = cmd1.ExecuteReader();
DataGridViewComboBoxColumn acname = dataGridView1.Columns[1] as DataGridViewComboBoxColumn;
while (rd1.Read())
{
acname.Items.Add(Convert.ToString(rd1["account_n"]));
}
}
actually i am using same data grid view for insert and update operation. i want that as user select different value from combo box list the default value should be replaced by selected one.
thanks in advance...
you can also bind the DataBindingComplete event. The only difference in previous and this answer is #Junaith bind the RowAdded event mean it will call the function every time you add the row. and here in mycase I bound DataBindingComplete event which will fire after the grid is full loaded mean this will call only once and that time it will check the all the checkbox in first column of the grid for all the lines.
mygrid.DataBindingComplete +=
new DataGridViewBindingCompleteEventHandler(mygrid_DataBindingComplete);
then and you have to define a function name as mygrid_DataBindingComplete
private void mygrid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
DataGridView temp = (DataGridView)sender;
foreach (DataGridViewRow rw in temp.Rows)
{
// here 0 indicating the checbox column is the first column in grid
// first column so index would be 0. true will check the checkbox
rw.Cells[0].Value = true;
}
}
CellFormatting event handler is not the right place to set the default value. CellFormatting occurs whenever the content of the cells needs to be formated for display. Whenever you the change the value in the combobox and accept the value, this event is raised and the default value code resets the user selection.
In DataGridView.RowsAdded you could set the defalut value for the combobox cell for the newly added row(s)
Sample Code:
void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
for (int i = e.RowIndex; i < (e.RowIndex+e.RowCount); i++)
{
dataGridView1.Rows[i].Cells[comboboxcolumn_index].Value = defaultvalue;
}
}
hai you can Just use this on RowAdded Event or DataGridview comboboxcolumn creation time. It will Set the Default Value
comboboxcolumn_Name.DefaultCellStyle.NullValue = "Please Select";
void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
for (int i = e.RowIndex; i < (e.RowIndex+e.RowCount); i++)
{
comboboxcolumn_Name.DefaultCellStyle.NullValue = "Please Select";
}
}
I have two tables.
tbl_Request(PKRequestID, RequestCode) and tbl_Personnel(PKPersonID, PerosnelName FKRequestID)
FKrfequestID is a foreign key to tbl_request.
I have a grid view in Requests.aspx page that shows tbl_request records. There is a "New Item" button in this page too. When user clicks this button RequestInsert.aspx page opens. In this page user enters some data like RequestCode and hits the "Next" button and goes to Personel.aspx page which contains a gridview that shows the personnel who are related to the request. In this page user should define Personnel who are related to the request. When the whole process finishes user hits "save" button. Both tables will be update when user clicks on "save" button. How can I implement Personel.aspx page?
HERE IS AN APPROACH :
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dttbl = new DataTable();
dttbl.Columns.Add("PKPersonID", System.Type.GetType("System.String"));
dttbl.Columns.Add("PerosnelName", System.Type.GetType("System.String"));
dttbl.Columns.Add("FKRequestID", System.Type.GetType("System.String"));
Session["MyDataTable"] = dttbl;
}
}
protected void btnok_Click(object sender, EventArgs e)
{
DataTable t = (DataTable)Session["MyDataTable"];
DataRow row1 = t.NewRow();
row1["PKPersonID"] = txtid.Text ;
row1["PerosnelName"] = txtname.Text;
row1["FKRequestID"] = Session["FKRequestID"];
t.Rows.Add(row1);
Session["MyDataTable"] = t;
GridView1.DataSource = t;
GridView1.DataBind();
}
protected void btnsave_Click(object sender, EventArgs e)
{
DataTable t2 = (DataTable)Session["MyDataTable"];
SqlConnection con = new SqlConnection("connection_string"
using (SqlCommand command = con.CreateCommand())
{
//Here you are inserting values to tbl_Request
if (con.State == 0)
con.Open();
command.CommandText = #"INSERT INTO tbl_Request (PKRequestID,RequestCode) VALUES (#PKRequestID,#RequestCode)";
command.Parameters.AddWithValue("#PKRequestID", Session["PKRequestID"]);
command.Parameters.AddWithValue("#RequestCode", Session["RequestCode"]);
command.ExecuteNonQuery();
}
foreach (DataRow row in t2.Rows)
{
//Here you are inserting values to tbl_Personnel
using (SqlCommand command2 = con.CreateCommand())
{
if (con.State == 0)
con.Open();
command2.CommandText = #"INSERT INTO tbl_Personnel (PerosnelName, FKRequestID) VALUES ( #PerosnelName, #FKRequestID)";
command2.Parameters.AddWithValue("#PerosnelName", txtname.Text);
command2.Parameters.AddWithValue("#FKRequestID", Session["PKRequestID"]);
command2.ExecuteNonQuery();
}
}
}
I have two tables.
tbl_Request(PKRequestID, RequestCode) and tbl_Personnel(PKPersonID, PerosnelName FKRequestID)
FKrfequestID is a foreign key to tbl_request. I have a grid view in Requests.aspx page that shows tbl_request records. There is a "New Item" button in this page too. When user clicks this button RequestInsert.aspx page opens. In this page user enters some data like RequestCode and hits the "Next" button and goes to Personel.aspx page which contains a gridview that shows the personnel who are related to the request. In this page user should define Personnel who are related to the request. When the whole process finishes user hits "save" button. Both tables will be update when user clicks on "save" button. How can I implement Personel.aspx page?
Here is the code for personnelaspx.cs code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dttbl = new DataTable();
dttbl.Columns.Add("PKPersonID", System.Type.GetType("System.String"));
dttbl.Columns.Add("PerosnelName", System.Type.GetType("System.String"));
dttbl.Columns.Add("FKRequestID", System.Type.GetType("System.String"));
Session["MyDataTable"] = dttbl;
}
}
protected void btnok_Click(object sender, EventArgs e)
{
DataTable t = (DataTable)Session["MyDataTable"];
DataRow row1 = t.NewRow();
row1["PKPersonID"] = txtid.Text ;
row1["PerosnelName"] = txtname.Text;
row1["FKRequestID"] = Session["FKRequestID"];
t.Rows.Add(row1);
Session["MyDataTable"] = t;
GridView1.DataSource = t;
GridView1.DataBind();
}
protected void btnsave_Click(object sender, EventArgs e)
{
DataTable t2 = (DataTable)Session["MyDataTable"];
SqlConnection con = new SqlConnection("connection_string"
using (SqlCommand command = con.CreateCommand())
{
//Here you are inserting values to tbl_Request
if (con.State == 0)
con.Open();
command.CommandText = #"INSERT INTO tbl_Request (PKRequestID,RequestCode) VALUES (#PKRequestID,#RequestCode)";
command.Parameters.AddWithValue("#PKRequestID", Session["PKRequestID"]);
command.Parameters.AddWithValue("#RequestCode", Session["RequestCode"]);
command.ExecuteNonQuery();
}
foreach (DataRow row in t2.Rows)
{
//Here you are inserting values to tbl_Personnel
using (SqlCommand command2 = con.CreateCommand())
{
if (con.State == 0)
con.Open();
command2.CommandText = #"INSERT INTO tbl_Personnel (PerosnelName, FKRequestID) VALUES ( #PerosnelName, #FKRequestID)";
command2.Parameters.AddWithValue("#PerosnelName", txtname.Text);
command2.Parameters.AddWithValue("#FKRequestID", Session["PKRequestID"]);
command2.ExecuteNonQuery();
}
}
}
How can I implement edit mode? In this mode I want users be able to delete personnel or add them?
Ok. You need to use session to store the tbl_Request.PKRequestID of the record that is to be edited when user clicks Edit button. After that re direct the user to a new page Edit_Requests_Personnel.aspx where you can edit the records.