I want to get grid view data in the StudentRegistrationForm. I have tried many times, but I am unable do this, and I don't understand. Can you tell me how I can get GridView values in my StudentRegistrationForm?
If I comment combo boxes code, then other values are put in StudentRegistrationForm, but I want to show all data in StudentRegistrationForm.
St_Registration obj = new St_Registration();
int St_Id = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
obj.txt_name.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
obj.txt_fname.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
obj.txt_sur_name.Text =dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
obj.txt_email.Text = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
obj.dob_date.Text = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
obj.date_of_registration.Text=dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString();
obj.txt_address.Text = dataGridView1.Rows[e.RowIndex].Cells[7].Value.ToString();
obj.rbtn_male.Checked = true;
obj.rbtn_female.Checked = false;
if (dataGridView1.Rows[e.RowIndex].Cells[8].Value.ToString() == "Female")
{
obj.rbtn_male.Checked = false;
obj.rbtn_female.Checked = true;
}
obj.txt_contact.Text = dataGridView1.Rows[e.RowIndex].Cells[9].Value.ToString();
obj.cmb_country.Text=dataGridView1.Rows[e.RowIndex].Cells[10].Value.ToString();
obj.cmb_province.Text = dataGridView1.Rows[e.RowIndex].Cells[11].Value.ToString();
obj.cmb_city.Text = dataGridView1.Rows[e.RowIndex].Cells[12].Value.ToString();
obj.txt_pin.Text=dataGridView1.Rows[e.RowIndex].Cells[13].Value.ToString();
obj.ShowDialog();
Related
I am currently working on a system. I have a datagridview with a contextmenu and an edit and delete button on it. I want to pass the value of the selected rows to a textbox when I click the edit on contextmenu.
I have successfully passed the value to the textbox but the only values that show are from the last inputted data to whatever row I click. I don't know how to get the id, can someone please help me fix my problem? :(
Here is my code:
private void BtnEdit_Click(object sender, EventArgs e)
{
frmAddEditStudent frm = new frmAddEditStudent(this);
cn.Open();
cm = new SqlCommand("SELECT s.studentID, s.studentNo, s.Lname, s.Fname, s.MI, s.gender, s.yearLevel, s.section, s.studImage, g.name, g.contactNo, g.address FROM Student s INNER JOIN Guardian g ON g.studentNo = s.studentNo WHERE g.studentNo = s.studentNo AND s.isActive = 'true' AND s.studentID = studentID", cn);
cm.Parameters.AddWithValue("studentID", lblID.Text);
for (int i = 0; i < guna2DataGridView1.Rows.Count; i += 1)
{
frm.btnSave.Enabled = false;
frm.lblTitle.Text = "Edit Student Details";
frm.lblID.Text = guna2DataGridView1.Rows[i].Cells[1].Value.ToString();
frm.txtStudentNo.Text = guna2DataGridView1.Rows[i].Cells[2].Value.ToString();
frm.txtLname.Text = guna2DataGridView1.Rows[i].Cells[3].Value.ToString();
frm.txtFname.Text = guna2DataGridView1.Rows[i].Cells[4].Value.ToString();
frm.txtMI.Text = guna2DataGridView1.Rows[i].Cells[5].Value.ToString();
frm.cboGradeLevel.Text = guna2DataGridView1.Rows[i].Cells[7].Value.ToString();
frm.cboSection.Text = guna2DataGridView1.Rows[i].Cells[8].Value.ToString();
frm.txtGuardianName.Text = guna2DataGridView1.Rows[i].Cells[9].Value.ToString();
frm.txtContactNo.Text = guna2DataGridView1.Rows[i].Cells[10].Value.ToString();
frm.txtAddress.Text = guna2DataGridView1.Rows[i].Cells[11].Value.ToString();
//Load Image
byte[] bytes = (byte[])guna2DataGridView1.Rows[i].Cells[12].Value;
MemoryStream ms = new MemoryStream(bytes);
frm.studImage.Image = Image.FromStream(ms);
//Retrieve gender value to radio button
if (guna2DataGridView1.Rows[i].Cells[6].Value.ToString() == "Male")
{
frm.rbMale.Checked = true;
}
else
{
frm.rbFemale.Checked = true;
}
}
cn.Close();
frm.ShowDialog();
It does not show up the data in the row that I selected, instead it only shows the last row in my database table.
You can get the current row or the selected rows from a datagridview in the following way (I think ID is cell with Index 1):
Console.WriteLine(guna2DataGridView1.CurrentRow.Cells[1].Value.ToString());
foreach (DataGridViewRow loRow in guna2DataGridView1.CurrentRow.SelectedRows)
{
Console.WriteLine(loRow.Cells[1].Value.ToString());
}
But you overwrite the form values in your loop every time.
It seems that your form can only display one row and not a collection.
And what about the command cm??
I have a student class which has all these fields and properties.
studentId, studentFirstname, studentLastname, test1Score, test2Score, studentMajor, typeOfStudent.
I computed the student grades and save them in a file. Now i want to display them in a datagridview, but i want to display: studentId, studentLastname, studentMajor, studentGrade.
Here's what my code looks like:
After i opened the open diaolg and get the file that i want to display.
The commented lines are the fields that i don't want to display. in my datagridview.
while (sLine != null)
{
string[] parts = sLine.Split(seps);
StudentGrade st = new StudentGrade();
st.Id = int.Parse(parts[0]);
//st.FirstName = parts[1];
st.LastName = parts[1];
// st.Test1 = int.Parse(parts[3]);
// st.Test2 = int.Parse(parts[4]);
st.Major = parts[2];
// st.TypeOfStudent = parts[6];
st.Grade = parts[3];
STList.Add(st);
sLine = sstr.ReadLine();
if (sLine == null)
break;
if (sLine.Trim() == "")
break;
}
dgStudents.DataSource = STList;
dgStudents.Refresh();
}
You can set Visible property of unwanted columns to false...
dgStudents.Columns["FirstName"].Visible = false;
dgStudents.Columns["Test1"].Visible = false;
dgStudents.Columns["Test2"].Visible = false;
dgStudents.Columns["TypeOfStudent"].Visible = false;
You can select required columns as below to fill the datagridview.
dgStudents.DataSource = STList.Select(col => new StudentGrade
{
Id = col.Id,
LastName = col.LastName,
Major = col.Major,
Grade = col.Grade
}.ToList();
I am trying to search the text(keywords) among the list which are bind to the repeater controller.I have a pagination implemented using the Data List. If I search the text, I will get the data source count as one, but if I check the result view "Enumeration yielded no results" error will come and the repeater will have no value
Here is my code
string authorname = AuthorNames();
string ProdTitle = ProductTitle();
string refno = ProductRefno();
List<Product> ProductList = GetProductDetails(categoryName, refno, ProdTitle, authorname);
pageDataSource.DataSource = ProductList;
pageDataSource.AllowPaging = true;
pageDataSource.PageSize = Convert.ToInt16(ddlPageSize.SelectedValue);
pageDataSource.CurrentPageIndex = CurrentPage;
lnkbtnNext.Enabled = !pageDataSource.IsLastPage;
lnkbtnPrevious.Enabled = !pageDataSource.IsFirstPage;
ViewState["totpage"] = pageDataSource.PageCount;
repproductlist.DataSource = pageDataSource;
repproductlist.DataBind();
DoPaging();
// Int32 value = Convert.ToInt32(ConfigurationManager.AppSettings["PaginationDisplayAt"]);
if (ProductList.Count > PaginationDisplayAt)
{
this.paginationDiv.Visible = true;
}
else
{
this.paginationDiv.Visible = false;
}
any help will be usefull
you probably need to create a new list from the search result before you bind it.
repproductlist.DataSource = pageDataSource.FindAll(x => x.Text.Contains(searchString)).ToList();
I have a standard DevExpress MVCxGridView bound to a DataTable, which is just a bunch of boolean values with the first column "SS" being a string code which is the datakey. I loop through all the columns and dynamically create the gridview columns. The grid that is displayed is a bunch of checkboxes which are options that can be configured.
I have a jquery js file that requires the data-* attributes to be set for these cells in order to inject the necessary functionality. I want to know how to add "data-*" attributes to each of the TD cells. "data-ss" being the datakey in the first column, and "data-wm" being the workmode in the column.
My Razor view code is as follows:
#model System.Data.DataTable
#{
var gv = Html.DevExpress().GridView(
settings =>
{
settings.Name = "gv";
settings.Enabled = true;
settings.KeyFieldName = "SS";
settings.CallbackRouteValues = new { Controller = "Test", Action = "DataBindingPartial" };
settings.Settings.HorizontalScrollBarMode = ScrollBarMode.Auto;
settings.Settings.VerticalScrollBarMode = ScrollBarMode.Auto;
settings.Settings.VerticalScrollableHeight = 200;
settings.SettingsPager.Mode = DevExpress.Web.ASPxGridView.GridViewPagerMode.ShowAllRecords;
MVCxGridViewBandColumn currentBand = null;
foreach (System.Data.DataColumn c in Model.Columns)
{
if (c.ColumnName == "SS")
{
DevExpress.Web.ASPxGridView.GridViewColumn column = settings.Columns.Add(c.ColumnName);
column.Caption = "SS";
column.CellStyle.CssClass = "ss_head";
column.HeaderStyle.CssClass = "ss_head_caption";
column.HeaderStyle.Cursor = "pointer";
}
else
{
// Get Column Definition retreives information based on the column name
// definition.ActivityType = "act" if activity or "dg" if DataGathering
// definition.WorkMode = abbreviated name of activity
// definition.Description = long description of activity
var definition =
TestModel.DefinitionColumn.GetColumnDefinition(c.ColumnName);
if (currentBand == null || currentBand.Name != definition.ActivityType)
{
currentBand = settings.Columns.AddBand();
currentBand.Name = definition.ActivityType;
currentBand.Caption = definition.ActivityType == "act" ? "Activity" : "Data Gathering";
currentBand.HeaderStyle.CssClass = String.Format("workmode_col workmode_{0}", definition.ActivityType);
}
DevExpress.Web.ASPxGridView.GridViewColumn column =
currentBand.Columns.Add(c.ColumnName, MVCxGridViewColumnType.CheckBox);
column.Caption = definition.WorkMode;
column.ToolTip = definition.Description;
column.Visible = true;
column.HeaderStyle.Cursor = "pointer";
column.CellStyle.CssClass = String.Format("workmode_{0} workmode_selectable workmode_col", definition.ActivityType);
column.HeaderStyle.CssClass = String.Format("workmode_{0} workmode_col", definition.ActivityType);
column.Width = 35;
}
}
});
var gvBound = gv.Bind(Model);
gvBound.Render();
}
Thank you Mikhail.
Using this I was able to add a settings configuration to set the data-* attributes:
settings.HtmlDataCellPrepared = (sender, e) =>
{
e.Cell.Attributes.Add(
"data-wm",
e.DataColumn.Caption
);
e.Cell.Attributes.Add(
"data-ssco",
e.KeyValue.ToString()
);
};
It is possible to use GridViewSettings.HtmlDataCellPrepared event to assign the required attributes. Check this SO thread.
first of all there is a searchbox form and a view form. after passing the value of the id in the searchbox, it should return all the values that matches with the id of that person after the textchange method occured. but it doesn't display a single value on the textboxes. here is my code
public void first_tab_search(string key)
{
key = txtSearch.Text;
var first = from a in dbcon.personal_informations where a.last_name == key select a;
foreach (var setThem in first)
{
txtsurname.Text = setThem.last_name;
txtfirstname.Text = setThem.first_name;
txtmiddlename.Text = setThem.middle_name;
txtID.Text = setThem.userid;
txtweight.Text = setThem.weight;
txttin.Text = setThem.tin;
txtsss.Text = setThem.sss;
txtaeno.Text = setThem.agency_employee_no;
txtbloodtype.Text = setThem.blood_type;
txtcitizenship.Text = setThem.citizenship;
txtcivilstatus.Text = setThem.civil_status;
txtcpno.Text = setThem.cell_no;
txtdob.Text = setThem.datetime_of_birth.ToString();
txtemail.Text = setThem.email_address;
txtgender.Text = setThem.sex;
txtgsis.Text = setThem.gsis_id;
txtheight.Text = setThem.height;
txtnameext.Text = setThem.name_ext;
txtpagibig.Text = setThem.pagibig_id;
txtpermaaddr.Text = setThem.permanent_address;
txtpermatelno.Text = setThem.permanent_telno;
txtpermazip.Text = setThem.permanent_zipcode;
txtphilhealth.Text = setThem.philhealth;
txtpob.Text = setThem.place_of_birth;
txtresidentialaddr.Text = setThem.residential_address;
txtresitelno.Text = setThem.residential_telno;
txtresizip.Text = setThem.residential_zipcode;
txtweight.Text = setThem.weight;
}
}
You have a whole host of problems going on here.
You pass a key into the method, and then immediately overwrite it with the contents of your search box.
Your search could return more than one result, and therefore your code is looping through each result and overwriting the output values with the last returned row. Use += rather than + in your loop, i.e.
txtsurname.Text += setThem.last_name;
Your code is currently case sensitive, this may be the desired approach but might not be.