Programmatically adding bound Gridview to tabPanels - c#

I want to add GridView programmatically in a TabPanel. In fact the tab panels themselves are created programmatically so I want to create a GridView for each TabPanel.
Here is the code to create the tab panels:
while (j < l.Count)
{
AjaxControlToolkit.TabPanel panel = new AjaxControlToolkit.TabPanel();
panel.HeaderText = testrub.RubricName.ToString();
GridView grid = new GridView();
grid.DataSource = element.GetElementByRubric(testrub.ID);
panel.Controls.Add(grid);
TabContainer1.Tabs.Add(panel);
j++;
}
As you can see the gridview that I want to create is databound. I would seriously appreciate some help. :)

Add this one line to your code
GridView grid = new GridView();
grid.DataSource = element.GetElementByRubric(testrub.ID);
grid.DataBind(); // <------ this new line

Related

how to add datagridview control to a radiobutton in the place of radiobuttonText in winforms

I want to add a datagridview control in the place of radiobutton text.The output should be like this
Circle DatagridviewControl
for that i written the code like this
DataGridView dgv = new DataGridView();
dgv.Columns.Add("column1", "id");
dgv.Columns.Add("column2", "name");
dgv.Rows.Add(1, "Anusha");
dgv.Rows.Add(2, "Anu");
rd.Controls.Add(dgv);
rd.AutoSize = true;
dgv.AutoSize = true;
But it is not displaying properly.
Please help me how to achieve the output i am fresher to the winforms
i don't understand why you want to do this. But if you really need it, just set left margin of DataGridView to some value,say 20. and then add this value to the width of RadioButton. don't forget to set radiobutton's autosize property to false.
`rd.AutoSize = false;
DataGridView dgv = new DataGridView();
dgv.Columns.Add("column1", "id");
dgv.Columns.Add("column2", "name");
dgv.Rows.Add(1, "Anusha");
dgv.Rows.Add(2, "Anu");
dgv.Left = 20; // adding the left margin
rd.Controls.Add(dgv);
rd.Height = dgv.Height;
rd.Width = dgv.Width + 20; //adding same margin to the radio button width`

.net Datagrid WinForm - Adding a DataGridViewButtonColumn Automatically Adds a Row

When adding a new DataGridViewButtonColumn to a DataGrid, a new row of buttons is added.
Is there a way to prevent this from happening?
I'd prefer not to have to clear all the rows after adding columns.
Also, can't use WPF. This is a Winform project I inherited.
DataGridViewButtonColumn bcol = new DataGridViewButtonColumn();
bcol.HeaderText = "Button Column";
bcol.Text = "Click Me";
bcol.Name = "btnClickMe";
The above code snippet creates a new row with a button.
Try disallowing adding new rows:
dataGridView1.AllowUserToAddRows = false;
I think what you're seeing is the new row at the bottom of your grid.
You can see the difference here: (enabled in the top pic, disabled in the bottom pic)

Need a sharepoint 2010 webpart to have editable GridView

I am a new sharepoint developer and trying to create webpart from VS2012 with sharepoint project workspace.
I have the below code in my workspace and want to show results in data grid view where i need last 2 columns to be editable. And then I need to submit the data to the sharepoint list.
protected override void CreateChildControls()
{
// Define the grid control that displays employee data in the Web Part.
grid = new DataGrid();
grid.Width = Unit.Percentage(100);
grid.GridLines = GridLines.Horizontal;
grid.HeaderStyle.CssClass = "ms-vh2";
grid.CellPadding = 2;
grid.BorderWidth = Unit.Pixel(5);
grid.HeaderStyle.Font.Bold = true;
grid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
// Populate the grid control with data in the employee data file.
try
{
var dataEndPoint = new Uri("http://<ServerName>/_vti_bin/listdata.svc");
var dc = new TeamSiteDataContext(dataEndPoint);
dc.Credentials = CredentialCache
.DefaultNetworkCredentials;
----
When I add the gridview to the Controls collection - ie Controls.Add(grid), I do not have control about which columns are editable.
You can use SPGridView to achieve that, here is an example
http://code.msdn.microsoft.com/office/Ejemplos-sobre-cmo-usar-el-69cd5f16

Disabling button in DataGridViewButtonColumn

I pull results from Oracle database and store it in DataTable before setting it as source in DataGridView. The reason for this is I remove some columns before I show it to user. Using DataGridViewButtonColumn I have added column that has enables user to "Edit" specific row. My problem is I'm trying to disable that button based on value in that current row. Leave the rest of the row buttons enabled.
var table = OracleConnection.Results(myquery);
table.Columns.Remove("Comments");
var editButton = new DataGridViewButtonColumn;
editButton.Value = "Edit";
editButton.Text = "Edit";
editButton.UseColumnTextForButtonValue = true;
_dtgvResults.Source = table;
_dtgvResults.Columns.Add(editButton);
Take a look in OnRowDataBound event in your DataGridView
MS Reference

Find Control inside Grid Row

i m using parent child grid and on child grid i m doing Show / hide threw java script. and child grid i bind run time with Templatecolumns like
GridView NewDg = new GridView();
NewDg.ID = "dgdStoreWiseMenuStock";
TemplateField TOTAL = new TemplateField();
TOTAL.HeaderTemplate = new BusinessLogic.GridViewTemplateTextBox(ListItemType.Header, "TOTAL",e.Row.RowIndex );
TOTAL.HeaderStyle.Width = Unit.Percentage(5.00);
TOTAL.ItemTemplate = new BusinessLogic.GridViewTemplateTextBox(ListItemType.Item, "TOTAL", e.Row.RowIndex);
NewDg.Columns.Add(TOTAL);
NewDg.DataSource = ds;
NewDg.DataBind();
NewDg.Columns[1].Visible = false;
NewDg.Columns[2].Visible = false;
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
NewDg.RenderControl(htw);
Now I have one TextBox inside Grid named "TOTAL" I want to Find This TextBox and wanna get its value.
How can Get it ?
You could get the TextBox control inside the corresponding cell of the GridView, using the Controls property or the FindControl(string id) method:
TextBox txtTotal = gv.Rows[index].cells[0].Controls[0] as TextBox;
or
TextBox txtTotal = gv.Rows[index].cells[0].Controls[0].FindControl("TOTAL") as TextBox;
where index could be 0 for the first row, or an iterator inside a for loop.
Alternatively, you can use a foreach loop over the GridView's rows:
foreach(GridViewRow row in gv.Rows)
{
TextBox txtTotal = row.cells[0].Controls[0].FindControl("TOTAL") as TextBox;
string value = txtTotal.Text;
// Do something with the textBox's value
}
Besides, you have to keep in mind that, if you're creating the GridView dynamically (and not declaratively in the web form), you won't be able to get this control after a page postback.
There is a great 4 Guys from Rolla article on the subject: Dynamic Web Controls, Postbacks, and View State
As you know you may get TextBox value for example for the first row and the first cell:
((TextBox) dgdStoreWiseMenuStock.Rows[0].Cells[0].Controls[1]).Text;
or change the index of control put 0 if the above line dosen't work.
Try This
TextBox txtTotal = (TextBox)gv.Rows[index].cells[0].FindControl("TOTAL");
string value = txtTotal.Text;
The easiest way to find the control inside the gridview use foreach loop to find the rows
foreach (GridViewRow row in Gridname.Rows)
{
TextBox txttotal = (TextBox)row.FindControl("textboxid_inside_grid");
string var = txttotal.Text;
Response.Write("Textbox value = " + var);
}

Categories